java多线程(一)线程状态与创建线程

来源:互联网 发布:c语言算法是什么意思 编辑:程序博客网 时间:2024/05/03 13:49

一、几个基本概念的区分

1.进程与程序的区分

程序进程包含代码部分、数据部分包含代码部分、数据部分、进程控制块顺序执行并发执行独占资源共享资源同步的异步的静态的动态的 (本质区别)

2.线程与进程的关系

相同:线程是轻量级的进程,一个进程可以包含一至多个线程

区别:

进程既是资源分配的单位有是独立运行的单位,线程只是资源分配的单位,只是运行的单位

建线程比进程开销小

3.线程的状态图



二、线程创建的两种方式:

1.直接从Thread类继承 2.实现Runnable接口创建一个任务类,然后 Thread t = new Thread(Runnable r)  即可创建一个线程

/* *1.实现Runnable接口重写run()方法来定义任务  */public class LiftOff implements Runnable {protected int countDown = 10;private static int taskCount = 0;private final int id = taskCount++;//用来区分多个任务的实例public LiftOff(){}public LiftOff(int countDown){this.countDown = countDown;}public String status(){return "#" + id + "(" + (countDown > 0 ? countDown : "Liftoff!") + "),   ";}@Overridepublic void run() {while(countDown-- > 0){System.out.println(status());Thread.yield();//让步,仅仅是暗示CPU,没有任何机制保证它将会被执行/*try {Thread.sleep(300);//睡眠,线程停止多少毫秒} catch (InterruptedException e) {e.printStackTrace();}*/}}public static void main(String[] args) {Thread t1 = new Thread(new  LiftOff(10));Thread t2 = new Thread(new  LiftOff(10));t1.setPriority(Thread.MAX_PRIORITY);//设置优先级,优先级高的相对获得的时间片多一点,并不保证    t1.start();    t2.start();    System.out.println(t1.getPriority());    System.out.println(t2.getPriority());    System.out.println("----------------------");}}

三、java.util.concurrent中常用类与接口

Callable:可以在任务完成时返回一个值 

Runnable:是执行工作的独立任务,但是它不返回任何值

Executor:具体Runnerable任务的执行者,中介对象

ExecutorService:一个线程池管理者,其实现类有多种。我们能把Runnable,Callable提交到池中让其调度(具有服务生命周期的Executor,例如关闭)

Executors:创建并返回ExecutorService等

Future:返回任务的上下文

1.下面介绍Executors的常用方法及区别

public class CachedThreadPool {public static void main(String[] args) {//ExecutorService exec = Executors.newFixedThreadPool(5);   //创建一个可重用固定大小线程集合的线程池(可一次性预先执行代价高昂的线程分配)ExecutorService exec = Executors.newCachedThreadPool();//创建一个可根据需要创建新线程的线程池(无界)for(int i = 0; i < 5; i++){exec.execute(new LiftOff());}exec.shutdown();}}

/* *就像是线程数量为1的FixedThreadPool,适用于在一个线程中连续运行的事物(原子性) *如果向它提交多个任务,那么这些任务将排队  */public class SingleThreadExecutor {public static void main(String[] args) {ExecutorService exec = Executors.newSingleThreadExecutor();for(int i= 0; i<5; i++)exec.execute(new LiftOff());exec.shutdown();}}

2.Runnable:是执行工作的独立任务,但是它不返回任何值,Callable:可以在任务完成时返回一个值 

ExecutorService.submit()方法会返回一个Future对象,Future的相关方法:

idDone():查询Future是否已经完成

cancel(boolean mayInterruptIfRunning)  //试图取消对任务的执行
get():当任务完成时,可以用get()返回结果

class TaskWithResults implements Callable<String>{public int id;public TaskWithResults(int id){this.id = id;}@Overridepublic String call() throws Exception {return "result of TaskWithResults: " + id;}}public class CallableDemo {public static void main(String[] args) {ExecutorService exec = Executors.newCachedThreadPool();List<Future<String>> results = new ArrayList<Future<String>>();for(int i = 0; i < 5; i++){results.add(exec.submit(new TaskWithResults(i)));}for (Iterator<Future<String>> iterator = results.iterator(); iterator.hasNext();) {Future<String> future = (Future<String>) iterator.next();try {System.out.println(future.get());} catch (InterruptedException | ExecutionException e) {e.printStackTrace();}finally{exec.shutdown();//启动一次顺序关闭,执行以前提交的任务,但不接受新任务}}}}


0 0
原创粉丝点击