java学习之浅谈多线程1

  1. 创建任务和线程
    任务就是对象,为了创建任务,必须首先为任务定义一个类。任务类必须实现Runnable接口。Runnable接口非常简单,它只有一个run方法。需要实现这个方法来告诉系统线程将如何运行。
    任务必须在线程中执行。Thread类包括创建线程的构造方法以及控制线程的很多有用的方法。
 1 +Thread()                       创建一个空线程
 2 +Thread(task:Runnable)          为指定任务创建一个线程
 3 
 4 +start():void                   启动线程使方法run()被JVM调用
 5 +isAlive():boolean              测试线程当前是否正在运行
 6 +setPriority(p:int):void        测试线程的优先级(范围从1到10)
 7 +join():void                    等待线程结束
 8 +sleep(millis:long):void        使线程睡眠指定的数
 9 +yield():void                   使线程暂停并允许执行其他线程
10 +interrupt():void               中断线程

使用下面的语句创建任务的线程:

1 Thread thread = new Thread(task);

然后调用start()方法告诉java虚拟机该线程准备允许,它会导致任务中的run()方法被执行。当run()方法执行完毕,线程就终止。

1 thread.start();

注意:因为Thread类实现了Runnable,所以,可以定义一个Thread的扩展类,并实现run方法,然后创建这个类的一个对象,并且调用它的start方法来启动线程。但是,不推荐使用这种方法,因为它将任务和运行任务的机制混在了一起。将任务从线程中分离出来是比较好的设计。

2.线程池
使用Thread类创建一个线程来运行任务对单一任务的执行是很方便的,但是由于必须为每一任务创建一个线程,因此对大量的任务而言是不够高效的。线程池是管理并开发执行任务个数的理想方法。java提供Executor接口来执行线程池中的任务,提供ExecutorService接口来管理和控制任务。ExecutorService是Executor的子接口。
Executor类:

1 +execute(Runnable object):void

ExecutorService类:

1 +shutdown():void    关闭执行器,但允许完成执行器中的任务。一旦关闭,它就不能接受新任务
2 +shutdownNow():List<Runnable>   即使线程池中还有未完成的线程,还是会立即关闭执行器。返回未完成任务的清单
3 +isShutdown():boolean   如果执行器已被关闭则返回true
4 +isTerminated():boolean 如果线程池中所有的任务都被终止,则返回true

为了创建一个Executor对象,可以使用Executors类中的静态方法。
Executors类:

1 +newFixedThreadPool(numberOfThreads:int):ExecutorService
2     创建一个线程池,该线程池可并发执行的线程数固定不变。
3     在线程的当前任务结束后,它可以被重用以执行另一个任务
4 +newCachedThreadPool():ExecutorService  
5     创建一个线程池,它可按需创建新线程,但当前面创建的线程可用时,
6     则重用它们

3.程序实例(打印字母100次)
使用Thread类:

 1 public class TaskThreadDemo {
 2 
 3     public static void main(String[] args) {
 4         PrintChar printA = new PrintChar('A', 100);
 5         PrintChar printB = new PrintChar('B', 100);
 6 
 7         Thread thread1 = new Thread(printA);
 8         Thread thread2 = new Thread(printB);
 9 
10         thread1.start();
11         thread2.start();
12     }
13 
14 }
15 
16 class PrintChar implements Runnable {
17 
18     private int times;
19     private char charToPrint;
20 
21     public PrintChar(char cha, int a) {
22         this.charToPrint = cha;
23         this.times =a;
24     }
25 
26     @Override
27     public void run() {
28         for(int i=0; i<times; i++) {
29             System.out.print(charToPrint + " ");
30         }
31     }
32 
33 }

使用线程池:

 1 import java.util.concurrent.ExecutorService;
 2 import java.util.concurrent.Executors;
 3 
 4 public class ExecutorDemo {
 5     public static void main(String[] args) {
 6         //创建一个线程池,该线程池可并发执行的线程数固定不变,在线程的当前任务结束后,它可以被重用以执行另一个任务
 7         ExecutorService executors = Executors.newFixedThreadPool(3);
 8 
 9         //创建一个线程池,它可以按需创建新线程,但当前面创建的线程可用时,则重用它们
10         ExecutorService executorService = Executors.newCachedThreadPool();
11 
12         executorService.execute(new PrintChar1('A', 100));
13         executorService.execute(new PrintChar1('B', 100));
14 
15         executorService.shutdown();
16 
17 //      executors.execute(new PrintChar1('A', 100));
18 //      executors.execute(new PrintChar1('B', 100));
19 
20 //      executors.shutdown();
21     }
22 }
23 
24 class PrintChar1 implements Runnable {
25 
26     private int times;
27     private char charToPrint;
28 
29     public PrintChar1(char cha, int a) {
30         this.charToPrint = cha;
31         this.times =a;
32     }
33 
34     @Override
35     public void run() {
36         for(int i=0; i<times; i++) {
37             System.out.print(charToPrint + " ");
38         }
39     }
40 
41 }
posted @ 2015-10-19 21:07  Devinzx  阅读(167)  评论(0编辑  收藏  举报