您的位置:首页 > 编程语言 > Java开发

关于JAVA多线程编程的一点笔记

2012-04-26 23:59 489 查看
1.Future类的使用:用于跟踪线程池中某一线程的执行状态

2.抽象类可以有构造方法,该构造方法可供子类的构造方法调用,但该抽象类不能直接实例化。

相关代码如下:

/**
* 类功能描述:
* Author:chenza
* Date:2012-4-26 下午10:23:36
*/
package test.tech.concurrent;

public interface IRunnableDemo extends Runnable {

public void execute() throws Exception;

}


/**
* 类功能描述:
* Author:chenza
* Date:2012-4-26 下午10:27:16
*/
package test.tech.concurrent;

public abstract class AbstractRunnableDemo implements IRunnableDemo {
public int id;

public AbstractRunnableDemo(){

}

public AbstractRunnableDemo(int id){
this.id = id;
}
@Override
public void run(){
System.out.println(Thread.currentThread().getName() + " start ...");
try {
this.execute();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " end!!!");
}

@Override
public abstract void execute() throws Exception;

}


/**
* 类功能描述:
* Author:chenza
* Date:2012-4-26 下午10:30:32
*/
package test.tech.concurrent;

public class RunnableDemoImpl1 extends AbstractRunnableDemo {

public RunnableDemoImpl1(){
super(110);
}

public RunnableDemoImpl1(int id){
super(id);
}

@Override
public void execute() throws Exception{
Thread.sleep(10000);
for(int i = 0;i < 5;i++){
System.out.println(Thread.currentThread().getName() + ":" + id + " === " + this.getClass().getName() + " [" + i + "]!");
}
}

}


类RunnableDemoImpl2、RunnableDemoImpl3与类RunnableDemoImpl1类似,只是暂停时间Thread.sleep()不一样。

/**
* 类功能描述:
* Author:chenza
* Date:2012-4-26 下午10:36:59
*/
package test.tech.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadPoolDemo implements Runnable {
private ExecutorService pool = Executors.newFixedThreadPool(5,Executors.defaultThreadFactory());

@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " start ...");
IRunnableDemo rd = null;
List<Future<?>> futures = new ArrayList<Future<?>>();
for(int i = 0;i < 10;i++){
if(i % 3 == 0){
rd = new RunnableDemoImpl1(111);
}else if(i % 3 == 1){
rd = new RunnableDemoImpl2(222);
}else if(i % 3 == 2){
rd = new RunnableDemoImpl3(333);
}
Future<?> future = pool.submit(rd);
futures.add(future);
}
try {
Thread.sleep(18000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(Future<?> future : futures){
System.out.println("Done or not : " + future.isDone());
}
System.out.println("start to shutdown thread pool ...");
pool.shutdown();//将会等线程池中的所有线程执行完后才关闭。
//pool.shutdownNow(); // 马上关闭,注意二者的区别
System.out.println("shutdown thread pool successful!!!");
System.out.println(Thread.currentThread().getName() + " end!!!");
}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + " start ...");
new Thread(new ThreadPoolDemo()).start();
//ThreadPoolDemo demo = new ThreadPoolDemo();
//demo.run();
System.out.println(Thread.currentThread().getName() + " end!!!");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: