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

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

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

    巷尾的酒吧

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      64 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks
    第一種:(Thread)

    package com.abin.lee.servlet.mythread.thread;

    public class Mythread extends Thread{
     private String name;
     public Mythread(String name) {
      this.name=name;
     }
     public void run() {
      synchronized(this.name){
       System.out.println("開始時間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
       System.out.println("name="+name);
       System.out.println("結束時間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
     
      }
     }
     
    }



    測試代碼:

    package com.abin.lee.servlet.mythread.thread;

    public class MythreadTest {
     public static void main(String[] args) {
      Mythread mythread1=new Mythread("ManyThread");
      Mythread mythread2=new Mythread("ManyThread");
      mythread1.start();
      mythread2.start();
     }

    }




    第二種:(Runnable)

    package com.abin.lee.servlet.mythread.runnable;

    public class MyRunnable implements Runnable{
     private String name;
     public MyRunnable(String name) {
      this.name=name;
     }
     public synchronized void run(){
       System.out.println("開始時間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
       System.out.println("name="+name);
       try {
        Thread.sleep(3000);
       } catch (InterruptedException e) {
        e.printStackTrace();
       }
       System.out.println("結束時間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
     }
    }




    測試代碼:

    package com.abin.lee.servlet.mythread.runnable;

    public class MyRunnableTest {
     public static void main(String[] args) {
      MyRunnable myRunnable=new MyRunnable("ManyThread");
      Thread thread1=new Thread(myRunnable);
      Thread thread2=new Thread(myRunnable);
      thread1.start();
      thread2.start();
     }
    }




    第三種:(Callable)這種說明一下,這個實現多線程的方式是在JDK1.5引進的,在java.util.concurrent.Callable  這個并發包下面,并且提供同步返回結果的多線程。

    package com.abin.lee.servlet.mythread.callable;

    import java.util.concurrent.Callable;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;

    public class MyCallable implements Callable<String>{
     private String name;
     public MyCallable(String name) {
      this.name=name;
     }
     public String call() throws Exception {
      Lock lock=new ReentrantLock();
      lock.lock();
      String result="";
      try {
       System.out.println("開始時間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
       System.out.println("name="+name);
       if(name.equals("ManyThread")){
        result="success";
       }else{
        result="failure";
       }
       try {
        Thread.sleep(3000);
       } catch (InterruptedException e) {
        e.printStackTrace();
       }
       System.out.println("結束時間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
     
      } catch (Exception e) {
       e.printStackTrace();
      }finally{
       lock.unlock();
      }

       return result;
      
     }
     
    }




    測試代碼:

    package com.abin.lee.servlet.mythread.callable;

    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;

    import org.junit.Test;

    public class MyCallableTest {
     @Test
     public void testMyCallable() throws InterruptedException, ExecutionException{
      ExecutorService executor=Executors.newFixedThreadPool(3);
      MyCallable myCallable=new MyCallable("ManyThread");
      Future future1=executor.submit(myCallable);
      System.out.println("future1="+future1.get());
      Future future2=executor.submit(myCallable);
      System.out.println("future2="+future2.get());
      Future future3=executor.submit(myCallable);
      System.out.println("future3="+future3.get());
      
     }

    }

    測試結果:

    開始時間:1350056647659,線程名字:pool-1-thread-1
    name=ManyThread
    結束時間:1350056650661,線程名字:pool-1-thread-1
    future1=success
    開始時間:1350056650661,線程名字:pool-1-thread-2
    name=ManyThread
    結束時間:1350056653661,線程名字:pool-1-thread-2
    future2=success
    開始時間:1350056653662,線程名字:pool-1-thread-3
    name=ManyThread
    結束時間:1350056656663,線程名字:pool-1-thread-3
    future3=success

     

    posted on 2012-10-12 23:43 abing 閱讀(283) 評論(0)  編輯  收藏 所屬分類: thread

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲色图综合在线| 国产精品酒店视频免费看| 亚洲无码黄色网址| 九九综合VA免费看| 亚洲高清免费视频| 天堂亚洲免费视频| 亚洲午夜爱爱香蕉片| 无码AV动漫精品一区二区免费| 免费又黄又硬又爽大片| 日韩毛片一区视频免费| 亚洲色欲或者高潮影院| 麻豆精品成人免费国产片| 国产V亚洲V天堂A无码| 久久免费线看线看| 亚洲精品电影天堂网| 韩国免费一级成人毛片| 亚洲香蕉在线观看| 国产成人免费手机在线观看视频| mm1313亚洲国产精品无码试看| 四虎影视永久免费视频观看| 一级黄色免费网站| 久久精品亚洲综合| 成人免费毛片内射美女-百度| 亚洲AV无码成人精品区日韩| mm1313亚洲精品无码又大又粗| 亚洲日韩激情无码一区| 99精品视频在线观看免费播放| 亚洲一卡二卡三卡| 免费在线观看毛片| 一个人免费视频观看在线www| 亚洲国产成人精品电影| 无人视频在线观看免费播放影院| 亚洲精品视频免费观看| 精品在线免费视频| 久久精品国产精品亚洲蜜月| 日韩免费精品视频| 人人爽人人爽人人片av免费| 亚洲精品在线不卡| 久久国产成人精品国产成人亚洲| 中文字幕在线观看免费视频 | 国产成人高清精品免费鸭子 |