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

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

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

    ゞ沉默是金ゞ

    魚(yú)離不開(kāi)水,但是沒(méi)有說(shuō)不離開(kāi)哪滴水.
    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 ゞ沉默是金ゞ 閱讀(854) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java SE
    主站蜘蛛池模板: 亚洲精品国产成人| 777成影片免费观看| 亚洲精品视频专区| 亚洲一区二区三区AV无码| 国产精品免费小视频| 18禁无遮挡无码国产免费网站| 精品免费久久久久国产一区 | 立即播放免费毛片一级| 亚洲日韩国产精品无码av| 狠狠综合久久综合88亚洲| 国产一级理论免费版| 成年人免费网站在线观看| 18观看免费永久视频| 黄色片免费在线观看| 国产免费黄色无码视频| 瑟瑟网站免费网站入口| 亚洲精品国产综合久久久久紧| 亚洲成AV人片久久| 亚洲视频免费一区| 亚洲av女电影网| 亚洲国产成人一区二区三区| 亚洲中文字幕无码久久2017| 国产精品亚洲视频| 亚洲第一黄色网址| 亚洲精品网站在线观看不卡无广告| 色在线亚洲视频www| 亚洲成人一级电影| 亚洲成aⅴ人在线观看| 亚洲嫩草影院在线观看| 亚洲精品国产免费| 亚洲日本乱码一区二区在线二产线 | 无码不卡亚洲成?人片| 免费日本黄色网址| 又色又污又黄无遮挡的免费视| 国产成人免费永久播放视频平台| 成年男女男精品免费视频网站 | 亚洲av无码片在线观看| 亚洲avav天堂av在线网爱情| 亚洲资源最新版在线观看| 国产成人精品日本亚洲网址| 精品亚洲456在线播放|