java学习之浅谈多线程1

来源:互联网 发布:java获取毫秒数 编辑:程序博客网 时间:2024/05/17 08:43
  1. 创建任务和线程
    任务就是对象,为了创建任务,必须首先为任务定义一个类。任务类必须实现Runnable接口。Runnable接口非常简单,它只有一个run方法。需要实现这个方法来告诉系统线程将如何运行。
    任务必须在线程中执行。Thread类包括创建线程的构造方法以及控制线程的很多有用的方法。
+Thread()                       创建一个空线程+Thread(task:Runnable)          为指定任务创建一个线程+start():void                   启动线程使方法run()被JVM调用+isAlive():boolean              测试线程当前是否正在运行+setPriority(p:int):void        测试线程的优先级(范围从110)+join():void                    等待线程结束+sleep(millis:long):void        使线程睡眠指定的数+yield():void                   使线程暂停并允许执行其他线程+interrupt():void               中断线程

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

Thread thread = new Thread(task);

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

thread.start();

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

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

+execute(Runnable object):void

ExecutorService类:

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

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

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

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

public class TaskThreadDemo {    public static void main(String[] args) {        PrintChar printA = new PrintChar('A', 100);        PrintChar printB = new PrintChar('B', 100);        Thread thread1 = new Thread(printA);        Thread thread2 = new Thread(printB);        thread1.start();        thread2.start();    }}class PrintChar implements Runnable {    private int times;    private char charToPrint;    public PrintChar(char cha, int a) {        this.charToPrint = cha;        this.times =a;    }    @Override    public void run() {        for(int i=0; i<times; i++) {            System.out.print(charToPrint + " ");        }    }}

使用线程池:

import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ExecutorDemo {    public static void main(String[] args) {        //创建一个线程池,该线程池可并发执行的线程数固定不变,在线程的当前任务结束后,它可以被重用以执行另一个任务        ExecutorService executors = Executors.newFixedThreadPool(3);        //创建一个线程池,它可以按需创建新线程,但当前面创建的线程可用时,则重用它们        ExecutorService executorService = Executors.newCachedThreadPool();        executorService.execute(new PrintChar1('A', 100));        executorService.execute(new PrintChar1('B', 100));        executorService.shutdown();//      executors.execute(new PrintChar1('A', 100));//      executors.execute(new PrintChar1('B', 100));//      executors.shutdown();    }}class PrintChar1 implements Runnable {    private int times;    private char charToPrint;    public PrintChar1(char cha, int a) {        this.charToPrint = cha;        this.times =a;    }    @Override    public void run() {        for(int i=0; i<times; i++) {            System.out.print(charToPrint + " ");        }    }}
0 0
原创粉丝点击