少年阿宾

那些青春的岁月

  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 阅读(924) 评论(0)  编辑  收藏 所属分类: JavaMultithread

只有注册用户登录后才能发表评论。


网站导航: