在java中,一个普通的类要成为一个可被java多线程机制调用的 "线程类" 有两种方式;继承Thread或者实现Runnable接口;如下所示:
class ThreadDemo extends Thread{
//定义一个变量,用来终止线程的运行
private int shutCount = 10;
private static int taskCount = 0 ;
private final int id = taskCount++;
//重写run方法
@Override
public void run() {
//一个退出循环,终止当前线程的条件
while( shutCount-- >0 ){
System.out.print(this);
//调用yield(),表示告诉线程调度器,我(当前线程)已经执行完毕了,你可以让另一个线程来执行了
//当然,这只是给调度器一个建议,并不一定有效
Thread.currentThread().yield();
}
}
@Override
public String toString() {
return "#"+id+"#"+shutCount+",";
}
}
class ThreadDemo implements Runnable{
//定义一个变量,用来终止线程的运行
private int shutCount = 10;
private static int taskCount = 0 ;
private final int id = taskCount++;
//实现run方法
@Override
public void run() {
//一个退出循环,终止当前线程的条件
while( shutCount-- >0 ){
System.out.print(this);
}
}
@Override
public String toString() {
return "#"+id+"#"+shutCount+"\r\n";
}
}
启动线程,测试一下:
public static void common(){
for (int i = 0; i < 2; i++) {
// 启动线程,适应于继承了Thread的写法
// new ThreadDemo().start();
// 启动线程,适用于实现Runnable接口的写法
new Thread( new ThreadDemo()).start();
}
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
common();
}
输出*********************************************************************************
#0#9,#1#9,#0#8,#1#8,#0#7,#1#7,#0#6,#1#6,#0#5,#1#5,
#0#4,#1#4,#0#3,#0#2,#0#1,#0#0,#1#3,#1#2,#1#1,#1#0,
**************************************************************************************
上面代码就是线程最基本的用法,创建以及启动,需要注意的是,必须调用线程类的start方法才是启动线程,调用run方法只是相当于调用一个普通的方法;
我们看到,线程0和线程1毫无规律的交替执行,直到终止循环的条件shutCount的值变为0时才停止执行;在后面我们将学会采用多种互斥机制以及java提供的高级并发工具类来让线程间的执行变得有条理,而不是像这样混乱不堪;
接下来我们看看使用线程池来管理调用线程;
//线程池调用
public static void executor(ExecutorService executor){
for (int i = 0; i < 3; i++) {
executor.execute(new ThreadDemo());
}
executor.shutdownNow();
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println("采用可根据需要自动创建新线程的线程池");
executor(Executors.newCachedThreadPool());
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println("采用固定大小线程池");
executor(Executors.newFixedThreadPool(2));
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println("采用单个线程的线程池");
executor(Executors.newSingleThreadExecutor());
}
输出*********************************************************************************
采用可根据需要自动创建新线程的线程池
#0#9,#1#9,#1#8,#1#7,#1#6,#1#5,#1#4,#1#3,#1#2,#1#1,
#1#0,#0#8,#0#7,#0#6,#0#5,#0#4,#2#9,#2#8,#0#3,#2#7,
#0#2,#0#1,#0#0,#2#6,#2#5,#2#4,#2#3,#2#2,#2#1,#2#0,
采用固定大小线程池
#3#9,#3#8,#3#7,#3#6,#3#5,#3#4,#3#3,#3#2,#3#1,#3#0,
#4#9,#4#8,#4#7,#4#6,#4#5,#4#4,#4#3,#4#2,#4#1,#4#0,
采用单个线程的线程池
#6#9,#6#8,#6#7,#6#6,#6#5,#6#4,#6#3,#6#2,#6#1,#6#0,
**************************************************************************************
我们在测试方法中通过Executors对象创建了3中不同形式的线程池,实际上这三种方式都是创建ThreadPoolExecutor对象,只不过通过Executors这个线程池构建工厂对要创建的ThreadPoolExecutor进行了不同的属性设置;
我们在executor方法中循环了3次,于是我们看到:
对于调用newCachedThreadPool()返回的线程池,它帮我们创建了3个线程,0,1,2 ;
对于调用newFixedThreadPool()返回的线程池,尽管我们循环了3此,但它只帮我们创建了2个线程,3,4;因为它是固定大小线程池;
对于调用newSingleThreadExecutor()返回的线程池,它只会创建一个线程;