少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(22)

          我參與的團(tuán)隊(duì)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          第一種:(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("結(jié)束時間:"+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("結(jié)束時間:"+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)這種說明一下,這個實(shí)現(xiàn)多線程的方式是在JDK1.5引進(jìn)的,在java.util.concurrent.Callable  這個并發(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("開始時間:"+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é)束時間:"+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());
            
           }

          }

          測試結(jié)果:

          開始時間:1350056647659,線程名字:pool-1-thread-1
          name=ManyThread
          結(jié)束時間:1350056650661,線程名字:pool-1-thread-1
          future1=success
          開始時間:1350056650661,線程名字:pool-1-thread-2
          name=ManyThread
          結(jié)束時間:1350056653661,線程名字:pool-1-thread-2
          future2=success
          開始時間:1350056653662,線程名字:pool-1-thread-3
          name=ManyThread
          結(jié)束時間:1350056656663,線程名字:pool-1-thread-3
          future3=success
          posted on 2012-10-12 23:41 abin 閱讀(932) 評論(0)  編輯  收藏 所屬分類: JavaMultithread
          主站蜘蛛池模板: 沁源县| 克什克腾旗| 满城县| 密云县| 金塔县| 叙永县| 阿拉尔市| 广安市| 通化市| 义马市| 牙克石市| 绥宁县| 赤水市| 四川省| 湘潭市| 无棣县| 花垣县| 大余县| 舒城县| 抚宁县| 多伦县| 谢通门县| 永德县| 泰顺县| 巨鹿县| 安岳县| 芜湖市| 延长县| 永定县| 甘肃省| 松江区| 高淳县| 璧山县| 藁城市| 临沭县| 石泉县| 镇坪县| 绥德县| 天津市| 荃湾区| 曲沃县|