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

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

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

    巷尾的酒吧

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      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("開始時(shí)間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
       System.out.println("name="+name);
       System.out.println("結(jié)束時(shí)間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
     
      }
     }
     
    }



    測(cè)試代碼:

    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("開始時(shí)間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
       System.out.println("name="+name);
       try {
        Thread.sleep(3000);
       } catch (InterruptedException e) {
        e.printStackTrace();
       }
       System.out.println("結(jié)束時(shí)間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
     }
    }




    測(cè)試代碼:

    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)這種說明一下,這個(gè)實(shí)現(xiàn)多線程的方式是在JDK1.5引進(jìn)的,在java.util.concurrent.Callable  這個(gè)并發(fā)包下面,并且提供同步返回結(jié)果的多線程。

    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("開始時(shí)間:"+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("結(jié)束時(shí)間:"+System.currentTimeMillis()+",線程名字:"+Thread.currentThread().getName());
     
      } catch (Exception e) {
       e.printStackTrace();
      }finally{
       lock.unlock();
      }

       return result;
      
     }
     
    }




    測(cè)試代碼:

    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());
      
     }

    }

    測(cè)試結(jié)果:

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

     

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

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲av午夜电影在线观看| 日韩精品无码免费专区午夜不卡| 国产成人高清精品免费软件| 美国免费高清一级毛片| 亚洲成AV人片在线播放无码| 成人女人A级毛片免费软件| 一级A毛片免费观看久久精品| 亚洲精品自产拍在线观看动漫| 在线a级毛片免费视频| 久久久免费观成人影院| 国产日本亚洲一区二区三区| 亚洲麻豆精品国偷自产在线91| 18级成人毛片免费观看| 免费看一级高潮毛片| 亚洲精品欧洲精品| 爱情岛论坛网亚洲品质自拍| 美女网站免费福利视频| 久久久久国色AV免费观看| 亚洲日韩精品无码AV海量| 亚洲AV日韩AV永久无码免下载 | xvideos永久免费入口| 亚洲毛片基地日韩毛片基地| 亚洲视频人成在线播放| 成人毛片免费观看| 久久精品乱子伦免费| 免费精品视频在线| 国产成人精品日本亚洲网址 | 亚洲国语精品自产拍在线观看| 四虎免费永久在线播放| 成人免费一级毛片在线播放视频| a毛片在线还看免费网站| 美女黄色毛片免费看| 亚洲一日韩欧美中文字幕在线| 亚洲一区二区在线免费观看| 亚洲色偷偷狠狠综合网| 国产精品视_精品国产免费 | 亚洲激情在线视频| 亚洲精品制服丝袜四区| 老司机亚洲精品影视www| 国产大片免费观看中文字幕| 可以免费看黄视频的网站|