<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ゞ沉默是金ゞ

    魚離不開水,但是沒有說不離開哪滴水.
    posts - 98,comments - 104,trackbacks - 0
    About Runnable:
    Runnable interface is implemented by the Thread class as well and it's a common protocol for all the objects who wish to execute in a different thread. It's one of the ways of creating threads in Java. The other way to create a thread is by subclassing the Thread class. A class implementing Runnable interface can simply pass itself to create a Thread instance and can run thereafter. This eliminates the need of subclassing the Thread class for the purpose of executing the code in a separate thread.
    As long as we don't wish to override other methods of the Thread class, it may be a better idea to implement the Runnable interface to enable multithreading capabilities to a class than enabling the same by extending the Thread class.

    About Callable:
    The designers of Java felt a need of extending the capabilities of the Runnable interface, but they didn't want to affect the uses of the Runnable interface and probably that was the reason why they went for having a separate interface named Callable in Java 1.5 than changing the already existing Runnable interface which has been a part of Java since Java 1.0.

    Similarities:
    1) Both threads can be used to cause a separate stack for a thread.

    2) Both has only one method : Inside Runnable it is called as
    public abstract void run( );
    Inside callable the method is being called as
    public abstract call() ;
    (Note : V can be any valid object in JAVA)

    Differences:

    1) Return Type -
    public abstract void run() --> Return type "void"
    public abstract call() --> Return type "Any valid JAVA Object"

    2) Run method can not throw any checked Exception, whereas
    public abstract call() throw checkedException
    Call method can throw any checked Exception.

    package com.jovialjava.blog.threads;

    import java.util.concurrent.Callable;
    import java.util.concurrent.FutureTask;

    /**
     * This program has been written to demonstrate the usage
     * of Runnable and Callable<String> Interfaces. Jovial Java makes
     * no guarantee of any harm or any problem caused by using this 
     * program.
     
    */
    public class Run_Vs_Call {
        
        
        
    public static void main(Stringargs){
            CallableTask call 
    = new CallableTask();
            RunnableTask run 
    = new RunnableTask();
            
    try{
                
    /*
                 * We need a Future task Launcher to launch Callable Class
                 
    */
                FutureTask
    <String> callTask = new FutureTask<String>(call);
                
    /*
                 * We need a Thread Launcher to to launch Runnable Class
                 
    */
                Thread runTask 
    = new Thread(run);
                callTask.run();
                runTask.start();
                
                
    /*
                 * Get is a blocking call, JVM will wait here until Call task
                 * finishes execution.
                 
    */
                System.out.println(callTask.get());
                
                
            }
    catch(Exception e){
                e.printStackTrace();
            }
            
        }
        
        
    /*
         *Callable<V> is defined as Callable<String>
         *
         
    */
        
    public static class CallableTask implements Callable<String>{
                    
            
    public String call( ){
                System.out.println(
    "Inside call method..!!");
                
    return "hello";
            }
        }    
        
    /*
         * Runnable Interface Implementation
         
    */
        
    public static class RunnableTask implements Runnable{
            
    public void run(){
                System.out.println(
    "Inside Run Method, I can not return any thing");
            }
        }

    }
    posted on 2012-08-06 10:24 ゞ沉默是金ゞ 閱讀(849) 評論(0)  編輯  收藏 所屬分類: Java SE
    主站蜘蛛池模板: 男女午夜24式免费视频| 亚洲日本久久久午夜精品| 在线视频网址免费播放| 国产乱子伦片免费观看中字| 亚洲精品人成网线在线播放va | 99亚洲乱人伦aⅴ精品| 全免费一级毛片在线播放| 亚洲精品9999久久久久无码| 日韩午夜免费视频| 无遮挡呻吟娇喘视频免费播放| 四虎影视免费永久在线观看| 日本高清免费中文在线看| 亚洲性日韩精品一区二区三区| 西西人体免费视频| 亚洲视频一区调教| 性做久久久久久久免费看| 日韩国产精品亚洲а∨天堂免| 亚洲国产免费综合| 国产精品免费看久久久 | 天堂亚洲免费视频| 毛片基地看看成人免费| 亚洲AV成人片色在线观看| 色老头永久免费网站| 亚洲av永久中文无码精品| 亚洲人成无码网站久久99热国产| 国产一级一毛免费黄片| 久久精品国产亚洲av水果派| 在线jlzzjlzz免费播放| 思思久久99热免费精品6| 精品亚洲成a人片在线观看| 国产精品免费视频一区| a毛片免费播放全部完整| 亚洲AV成人噜噜无码网站| 亚洲欧洲日产国码高潮αv| 日韩精品内射视频免费观看 | 白白色免费在线视频| 午夜亚洲AV日韩AV无码大全| 免费毛片在线视频| 日本黄色动图免费在线观看| 国产亚洲欧美在线观看| 亚洲黄色在线电影|