java多线程并发库_Java多线程与并发库

同步方式

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjavax.xml.stream.events.StartDocument;public classTestSynchronized {public static voidmain(String[] args) {//TODO Auto-generated method stub

TestSynchronized test = newTestSynchronized();

test.init();

}voidinit() {final Outputer outputer = newOutputer();new Thread(newRunnable(){

@Overridepublic voidrun() {//TODO Auto-generated method stub

while(true){try{

Thread.sleep(10);

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

outputer.Output("wangyuyuyuyuyuyuyuyu");

}

}

}){

}.start();new Thread(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

while(true){try{

Thread.sleep(10);

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

outputer.Output("zhouzhanzhaozhaozhaozhao");

}

}

}){

}.start();

}classOutputer{final static String lockKey = "lock";//定义输出函数//第一种方式,提供某个锁

public voidOutput(String name){int len =name.length();synchronized(lockKey) {for (int i = 0; i < len; i++){

System.out.print(name.charAt(i));

}

System.out.println();

}

}//第二种方式,锁住自己

public voidOutput1(String name){int len =name.length();synchronized (this) { //也可以用Outputer.class

for (int i = 0; i < len; i++){

System.out.print(name.charAt(i));

}

System.out.println();

}

}//第三种方式,函数前面加关键字synchronized

public synchronized voidOutput2(String name){int len =name.length();for (int i = 0; i < len; i++){

System.out.print(name.charAt(i));

}

System.out.println();

}

}

}

View Code

定时器Timer

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.Timer;importjava.util.TimerTask;public classTimerTest {public static voidmain(String[] args){new Timer().schedule(newTimerTask(){

@Overridepublic voidrun() {//TODO Auto-generated method stub

System.out.println("boom");

}

},3000);

}

}

View Code

HashMap存储线程

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.HashMap;importjava.util.Map;importjava.util.Random;public classTestThreadMap {private static HashMap map = new HashMap();public static voidmain(String[] args) {//TestThreadMap testThreadMap = new TestThreadMap();

for (int i = 0; i < 2; i++) {new Thread(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

int data = newRandom().nextInt();

map.put(Thread.currentThread(), data);newA().Get();newB().Get();

}

}){}.start();

}

}static classA{public voidGet(){int data =map.get(Thread.currentThread());

System.out.println("A from " + Thread.currentThread().getName() + " get data " +data);

}

}static classB{public voidGet(){int data =map.get(Thread.currentThread());

System.out.println("B from " + Thread.currentThread().getName() + " get data " +data);

}

}

}

View Code

ThreadLocal类似HashMap存储线程

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.HashMap;importjava.util.Map;importjava.util.Random;public classTestThreadMap {private static HashMap map = new HashMap();public static voidmain(String[] args) {//TestThreadMap testThreadMap = new TestThreadMap();

for (int i = 0; i < 2; i++) {new Thread(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

int data = newRandom().nextInt();

map.put(Thread.currentThread(), data);newA().Get();newB().Get();

}

}){}.start();

}

}static classA{public voidGet(){int data =map.get(Thread.currentThread());

System.out.println("A from " + Thread.currentThread().getName() + " get data " +data);

}

}static classB{public voidGet(){int data =map.get(Thread.currentThread());

System.out.println("B from " + Thread.currentThread().getName() + " get data " +data);

}

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.Random;public classTestThreadlocal_2 {public static voidmain(String[] args) {//TODO Auto-generated method stub

for (int i = 0; i < 2; i++){new Thread(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

int data = newRandom().nextInt();

MyData.getInstance().setName("myData" +data);

MyData.getInstance().setAge(data);newA().Get();newB().Get();

}

}){}.start();

}

}static classA{public voidGet(){

MyData data=MyData.getInstance();

System.out.println("A from " + Thread.currentThread().getName() + " get data " + data.getName() + ", " +data.getAge());

}

}static classB{public voidGet(){

MyData data=MyData.getInstance();

System.out.println("B from " + Thread.currentThread().getName() + " get data " + data.getName() + ", " +data.getAge());

}

}

}classMyData{

String name;intage;public static /*synchronized*/MyData getInstance(){

MyData instance=threadLocal.get();if (instance == null){ //不存在就创建一个与本线程有关的实例对象

instance = newMyData();

threadLocal.set(instance);

}returninstance;

}//private static MyData instance = null;

private static ThreadLocal threadLocal = new ThreadLocal();publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}

}

View Code

线程池

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.TimeUnit;public classTestThreadPool {public static voidmain(String[] args){//固定大小的线程池

ExecutorService threadPool = Executors.newFixedThreadPool(3);//缓存线程池,当线程不够用时会自动增加,多了会自动减少//ExecutorService threadPool = Executors.newCachedThreadPool();//单一线程池,线程死了可以重新启动//ExecutorService threadPool = Executors.newSingleThreadExecutor();

for (int i = 1; i <= 10; i++){final int taskid =i;

threadPool.execute(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

for (int j = 1; j <= 10; j++) {

System.out.println(Thread.currentThread().getName()+ " is loop of " + j + " for task of " +taskid);

}

}

});

}

System.out.println("all have finished");

threadPool.shutdown();//线程池里没有任务了,线程池才关闭,等10个任务都完成后才关闭//threadPool.shutdownNow();//一个线程完成之后立马关闭,此时只完成了3个任务

/*//定时器线程池

Executors.newScheduledThreadPool(3).schedule(

new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

System.out.println("booming");

}

},

6,

TimeUnit.SECONDS); //多长时间后执行任务*/Executors.newScheduledThreadPool(3).scheduleAtFixedRate( //以固定频率执行任务

newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

System.out.println("booming");

}

},6, //初始时间

2, //间隔时间

TimeUnit.SECONDS);

}

}

View Code

锁Lock

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.concurrent.locks.Lock;importjava.util.concurrent.locks.ReentrantLock;importjavax.xml.stream.events.StartDocument;public classTestLock {public static voidmain(String[] args) {//TODO Auto-generated method stub

TestLock test = newTestLock();

test.init();

}voidinit() {final Outputer outputer = newOutputer();new Thread(newRunnable(){

@Overridepublic voidrun() {//TODO Auto-generated method stub

while(true){try{

Thread.sleep(10);

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

outputer.Output("wangyuyuyuyuyuyuyuyu");

}

}

}){

}.start();new Thread(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

while(true){try{

Thread.sleep(10);

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

outputer.Output("zhouzhanzhaozhaozhaozhao");

}

}

}){

}.start();

}classOutputer{

Lock lock= new ReentrantLock(); //锁

public voidOutput(String name){int len =name.length();

lock.lock();try{for (int i = 0; i < len; i++){

System.out.print(name.charAt(i));

}

System.out.println("");

}finally{//TODO: handle exception

lock.unlock();

}

}

}

}

View Code

读写锁ReadWriterLock

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.HashMap;importjava.util.concurrent.locks.ReadWriteLock;importjava.util.concurrent.locks.ReentrantReadWriteLock;public classTestReadWriterLock {

HashMap mp = new HashMap();private ReadWriteLock rwl = newReentrantReadWriteLock();publicObject Get(String key){

rwl.readLock().lock();

Object value= null;try{

value=mp.get(key);if (value == null) {

rwl.readLock().unlock();

rwl.writeLock().lock();try{if (value == null){

value= "aaa";

}

}finally{//TODO: handle finally clause

rwl.writeLock().unlock();

}

rwl.readLock().lock();

}

}finally{

rwl.readLock().unlock();

}returnvalue;

}

}

View Code

信号灯Semaphere(控制当前运行的线程个数)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Semaphore;importjava.util.function.IntToDoubleFunction;public classTestSemaphere {public static voidmain(String[] args) {//TODO Auto-generated method stub

/*1.创建线程池

*2.创建信号灯,大小为3

*3.循环10次,Runnable里设置信号灯acqure*/ExecutorService es=Executors.newCachedThreadPool();

Semaphore semaphore= new Semaphore(3);for(int i = 0; i < 10; i++){

es.execute(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

try{

semaphore.acquire();

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

System.out.println("线程" + Thread.currentThread().getName() + "进入");try{

Thread.sleep((int)Math.random() * 10000);

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

System.out.println("线程" + Thread.currentThread().getName() + "即将结束");

semaphore.release();

System.out.println("线程" + Thread.currentThread().getName() + "已结束");

es.shutdown();

}

});

}

}

}

View Code

数据交换Exchanger(当两个数据都到达后才能进行交换,否则阻塞)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.concurrent.Exchanger;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;public classTestExchanger {public static voidmain(String[] args) {//TODO Auto-generated method stub

ExecutorService es =Executors.newCachedThreadPool();

Exchanger exchanger = new Exchanger();

es.execute(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

try{

String data1= "x";

System.out.println("线程" + Thread.currentThread().getName() + "正在把数据" + data1 + "发送出去");

Thread.sleep((int)(Math.random() * 5000));

String getData=exchanger.exchange(data1);

System.out.println("线程" + Thread.currentThread().getName() + "接受到的数据为: " +getData);

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

}

});

es.execute(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

try{

String data1= "y";

System.out.println("线程" + Thread.currentThread().getName() + "正在把数据" + data1 + "发送出去");

Thread.sleep((int)(Math.random() * 5000));

String getData=exchanger.exchange(data1);

System.out.println("线程" + Thread.currentThread().getName() + "接受到的数据为: " +getData);

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

}

});

}

}

View Code

同步屏障CyclicBarrier(多个线程彼此等待,集合后再往后运行)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.concurrent.CyclicBarrier;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;public classTestCyclicBarrier {//多个线程彼此等待,集合后再运行

public static voidmain(String[] args) {//TODO Auto-generated method stub//创建线程池和CyclicBarrier,同时运行多个线程,调用await

ExecutorService es=Executors.newCachedThreadPool();final CyclicBarrier cb = new CyclicBarrier(3);for (int i = 0; i < 3; i++){

es.execute(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

try{

Thread.sleep((int)(Math.random() * 1000));

System.out.println("线程" + Thread.currentThread().getName() + "到达集合点1");

System.out.println("当前有" + (cb.getNumberWaiting()+1) + "人在等待");

cb.await();

Thread.sleep((int)(Math.random() * 1000));

System.out.println("线程" + Thread.currentThread().getName() + "到达集合点2");

System.out.println("当前有" + (cb.getNumberWaiting()+1) + "人在等待");

cb.await();

Thread.sleep((int)(Math.random() * 1000));

System.out.println("线程" + Thread.currentThread().getName() + "到达集合点3");

System.out.println("当前有" + (cb.getNumberWaiting()+1) + "人在等待");

cb.await();

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

}

});

}

es.shutdown();

}

}

View Code

CountDownLatch(类似倒计时计数器,当计数减为0时,所有等待者才开始执行)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.concurrent.CountDownLatch;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;public classTestCountDownLatch {//类似倒计时计数器,当计数减为0的时候,所有等待者才开始执行

public static voidmain(String[] args) {//TODO Auto-generated method stub//创建两个计数器,一个为1,一个为3,代表一个裁判员,三个运动员,裁判员在未下达命令前运动员等待,下达命令后才执行//等三个运动员都到达终点后,裁判员才公布成绩

ExecutorService es =Executors.newCachedThreadPool();

CountDownLatch cdOrder= new CountDownLatch(1);

CountDownLatch cdAnswer= new CountDownLatch(3);for (int i = 0; i < 3; i++) {final int id =i;

es.execute(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

try{

System.out.println("运动员" + id + "正准备接受命令");

cdOrder.await();

System.out.println("运动员"+ id + "接受到命令");

Thread.sleep((int)(Math.random() * 5000));

System.out.println("运动员" + id + "到达终点");

cdAnswer.countDown();

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

}

});

}try{

Thread.sleep(1000);

System.out.println("裁判员发出指令");

cdOrder.countDown();

System.out.println("裁判员等待所有运动员到达终点");

cdAnswer.await();

System.out.println("裁判员公布成绩");

}catch(Exception e) {//TODO: handle exception

}

}

}

View Code

Callable和Future

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.Random;importjava.util.concurrent.Callable;importjava.util.concurrent.CompletionService;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.ExecutorCompletionService;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Future;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;public classTestCallableAndFuture {public static void main(String[] args) throwsInterruptedException, ExecutionException, TimeoutException{

ExecutorService threadPool=Executors.newSingleThreadExecutor();

Future future=threadPool.submit(new Callable() {

@Overridepublic String call() throwsException {//TODO Auto-generated method stub

Thread.sleep(2000);return "hello";

}

});

System.out.println("得到结果: " + future.get()); //callable完成任务返回结果,由future去拿,需要等待一段时间//System.out.println("得到结果: " + future.get(1, TimeUnit.SECONDS));//要在规定的时间内得到结果,如果得不到就抛出异常//CompletionService 用于提交一组callable任务,并用take方法得到一个已完成任务的future对象

ExecutorService threadPool2 = Executors.newFixedThreadPool(10);

CompletionService completionService = new ExecutorCompletionService(threadPool2);for (int i = 1; i <= 10; i++){final int taskid =i;

completionService.submit(new Callable() {

@Overridepublic Integer call() throwsException {//TODO Auto-generated method stub

try{

Thread.sleep(new Random().nextInt(5000));

}catch(Exception e) {//TODO: handle exception

}returntaskid;

}

});

}for (int i = 1; i <= 10; i++){

System.out.println(completionService.take().get());

}

}

}

View Code

阻塞队列ArrayBlockingQueue

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.util.concurrent.ArrayBlockingQueue;public classTestArrayBlockingQueue {//阻塞队列当队列为空时take会阻塞,当队列满时put会阻塞//用两个阻塞队列模拟两个线程交替运行//两个阻塞队列大小均设置为1,其中一个放一个数据

public static voidmain(String[] args) {//TODO Auto-generated method stub

Business business = newBusiness();new Thread(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

for (int j = 1; j <= 10; j++) {

business.Sub(j);

}

}

}){}.start();for (int j = 1; j <= 10; j++) {

business.Main(j);

}

}static classBusiness{

ArrayBlockingQueue abq1= new ArrayBlockingQueue(1);

ArrayBlockingQueue abq2= new ArrayBlockingQueue(1);publicBusiness()

{try{

abq2.put(1);

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

}public void Sub(intj){try{

abq1.put(1);for (int i = 1; i <= 10; i++){

System.out.println("sub thread " + i + " of loop " +j);

}

abq2.take();

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

}public void Main(intj){try{

abq2.put(1);for (int i = 1; i <= 10; i++){

System.out.println("main thread " + i + " of loop " +j);

}

abq1.take();

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

}

}

}

View Code

练习: 子线程运行10次,主线程运行20次,交替运行

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

public classprictice_1 {//子线程运行10次,主线程运行20次

public static voidmain(String[] args) {//TODO Auto-generated method stub

Business business = newBusiness();new Thread(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

for (int j = 1; j <= 10; j++) {

business.Sub(j);

}

}

}){}.start();for (int j = 1; j <= 10; j++) {

business.Main(j);

}

}

}classBusiness{private boolean isSub = true;public synchronized void Sub(intj){while (!isSub) {try{this.wait();

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

}for (int i = 1; i <= 10; i++){

System.out.println("sub thread " + i + " of loop " +j);

}

isSub= false;this.notify();

}public synchronized void Main(intj){while(isSub){try{this.wait();

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

}for (int i = 1; i <= 20; i++){

System.out.println("main thread " + i + " of loop " +j);

}

isSub= true;this.notify();

}

}

View Code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值