<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
    主站蜘蛛池模板: 久久久久久久岛国免费播放| 日韩一级片免费观看| 污污网站18禁在线永久免费观看| 亚洲免费日韩无码系列| 一级视频在线免费观看| 在线亚洲97se亚洲综合在线| 两个人看的www免费高清| 亚洲综合国产一区二区三区| 午夜精品射精入后重之免费观看| 国产亚洲精品国产| 久久免费的精品国产V∧| 亚洲第一香蕉视频| 成人性生活免费视频| 国产亚洲人成在线影院| 亚洲国产无套无码av电影| 久久久久久毛片免费播放| 亚洲成A人片在线播放器| 免费a级毛片18以上观看精品| 日韩一级片免费观看| 亚洲AV无码成人精品区在线观看 | 精品无码国产污污污免费网站| 久久久久亚洲精品日久生情| 免费做爰猛烈吃奶摸视频在线观看| 亚洲一区二区三区国产精华液| 国产免费AV片无码永久免费| h视频在线观看免费| 亚洲国产人成在线观看69网站| 免费做爰猛烈吃奶摸视频在线观看| 极品美女一级毛片免费| 国产午夜精品免费一区二区三区| 亚洲一区二区电影| 日韩电影免费在线观看视频| 久久九九免费高清视频| 亚洲一区二区久久| 亚洲偷自拍拍综合网| 亚洲免费黄色网址| 乱爱性全过程免费视频| 亚洲综合久久成人69| 亚洲精品97久久中文字幕无码| 日本人的色道免费网站| eeuss影院免费直达入口|