多线程的简单认识

来源:互联网 发布:怎么把数组转化成json 编辑:程序博客网 时间:2024/05/21 12:40

最近看了一些资料再结合自己的积累,说一点多线程的简单认识,能力有限,如有纰漏,欢迎指正

线程:相比于进程来说,可以称为轻量级的进程,是程序能执行的最小单元。1个进程可以有多个线程,1个服务或者程序也可以有多个线程。

线程有5种状态,创建,就绪,运行,堵塞,停止。

线程的创建分为2种:

第一种,无返回的线程,继承Thread,实现Runnable接口,其中Thread底层还是实现Runnable接口,但是又做了一些封装,包括添加线程优先级别(1-10,一般的都是5),创建线程本地副本ThreadLocalMap,创建线程id,线程name等。

第二种,有返回的线程,实现callable<T>接口,或者实现Future<T>接口,其中callable接口可以返回自定义的接口,或者运行的异常,Future接口不但能有callable功能,而且还能取消执行,查询是否完成,添加对线程结果的监听等。

线程的运行是start方法,执行逻辑需要重写run方法,线程的堵塞可以有单线程的sleep,yield,join;多线程的countDownLatch,cyclicBarrier等

其中区别:

sleep 线程睡眠,可以指定睡眠的时间,线程睡眠后,不在拥有CPU资源,在睡眠时,比当前线程级别低的线程可以获取当前CPU资源来执行。当睡眠醒来时,会重新申请CPU资源,此时线程处于就绪状态。

yield  线程屈服,无法指定睡眠时间,释放CPU资源,但是不放弃所拥有的资源这点和sleep一样,但是在执行此方法后,比当前线程优先级高的或者相等的线程会执行。

join 堵塞线程,直到当前线程die之后,再执行。

countDownLatch  多线程同步,CountDownLatch cdl = new CountDownLatch(N);一个或者多个线程等待另外N个线程执行完毕后,再执行。

简单示例:

package test.com.wangdong.concurrent;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class CountDownLatchTest {

private static ExecutorService es = Executors.newFixedThreadPool(3);
private static CountDownLatch cdl = new CountDownLatch(3);

public static void main(String[] args) throws InterruptedException {
CountDownLatchTest test =  new CountDownLatchTest();
System.out.println("主裁判喊口号----准备");
es.execute(test.new Runner(cdl, "number1 "));
es.execute(test.new Runner(cdl, "number2 "));
es.execute(test.new Runner(cdl, "number3 "));
cdl.await();
System.out.println("比赛开始......");
es.shutdown();
}


class Runner implements Runnable{
private CountDownLatch latch = null;
private String name ;


public Runner(CountDownLatch latch, String name) {
this.latch = latch;
this.name = name;
}




public void run() {
try {
Thread.sleep((long) (Math.random()*100));
latch.countDown();
System.out.println(name+" 准备ok");


} catch (InterruptedException e) {
e.printStackTrace();
}

}
}

}

CyclicBarrier   多线程同步,CyclicBarrier cb = new CyclicBarrier(M); M个线程相互等待,直到某个线程执行完之后,它们才能执行。

简单示例:

package test.com.wangdong.concurrent;


import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class CyclicBarrierTest {
private static ExecutorService es = Executors.newFixedThreadPool(3);
static CyclicBarrier cb = new CyclicBarrier(3);

public static void main(String[] args) {
CyclicBarrierTest cbt = new CyclicBarrierTest();
es.execute(cbt.new Runner("runner 1",cb));
es.execute(cbt.new Runner("runner 2",cb));
es.execute(cbt.new Runner("runner 3",cb));

es.shutdown();
}


class Runner implements Runnable{


private String name;
private CyclicBarrier barrier = null;

public Runner(String name,CyclicBarrier barrier) {
this.name = name ;
this.barrier = barrier;
}




@Override
public void run() {
try {
Thread.sleep((long) (Math.random()*100));
System.out.println(name +" 准备ok");
barrier.await();
System.out.println(name + " runing");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}

}

}

}

原创粉丝点击