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

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

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

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      500 Posts :: 0 Stories :: 135 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:41 abin 閱讀(936) 評論(0)  編輯  收藏 所屬分類: JavaMultithread
    主站蜘蛛池模板: 国产AⅤ无码专区亚洲AV| 成人特黄a级毛片免费视频| 日韩在线看片免费人成视频播放| 久久亚洲日韩看片无码| 午夜无码A级毛片免费视频| 亚洲中文字幕第一页在线| 黄视频在线观看免费| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 免费国产污网站在线观看15| 亚洲乱码精品久久久久..| eeuss免费影院| 日本红怡院亚洲红怡院最新 | 亚洲一久久久久久久久| 成人无遮挡裸免费视频在线观看 | 色老板亚洲视频免在线观| 亚洲人成电影网站国产精品| 美女黄频视频大全免费的| 亚洲AV无码乱码在线观看性色扶| 羞羞网站免费观看| 亚洲美女在线国产| 光棍天堂免费手机观看在线观看| 亚洲一二成人精品区| 国产成人精品免费午夜app| 亚洲人成自拍网站在线观看 | 免费AA片少妇人AA片直播| 亚洲日韩AV一区二区三区中文| 国产精品高清全国免费观看| 一级成人生活片免费看| 成年女人18级毛片毛片免费| MM1313亚洲精品无码久久| 国产亚洲色婷婷久久99精品91| 久久久久久成人毛片免费看| 亚洲乱码在线视频| 亚洲国产一区二区三区| 久久精品视频免费看| 国产a v无码专区亚洲av| 久久精品成人免费观看| 亚洲中文字幕一二三四区| 精品亚洲一区二区三区在线观看| 99热这里只有精品免费播放| 男人的天堂av亚洲一区2区|