Java多线程与并发库

同步方式
import javax.xml.stream.events.StartDocument;public class TestSynchronized {public static void main(String[] args) {// TODO Auto-generated method stubTestSynchronized test = new TestSynchronized();test.init();}void init() {final Outputer outputer = new Outputer();new Thread(new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){try {Thread.sleep(10);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}outputer.Output("wangyuyuyuyuyuyuyuyu");}}}){}.start();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){try {Thread.sleep(10);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}outputer.Output("zhouzhanzhaozhaozhaozhao");}}}){}.start();}class Outputer{final static String lockKey = "lock";//定义输出函数//第一种方式,提供某个锁public void Output(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 void Output1(String name){int len = name.length();synchronized (this) { //也可以用Outputer.classfor (int i = 0; i < len; i++){System.out.print(name.charAt(i));}System.out.println();}}//第三种方式,函数前面加关键字synchronizedpublic synchronized void Output2(String name){int len = name.length();for (int i = 0; i < len; i++){System.out.print(name.charAt(i));}System.out.println();}}}
定时器Timer
import java.util.Timer;
import java.util.TimerTask;public class TimerTest {public static void main(String[] args){new Timer().schedule(new TimerTask(){@Overridepublic void run() {// TODO Auto-generated method stubSystem.out.println("boom");}}, 3000);}
}
HashMap存储线程
import java.util.HashMap;
import java.util.Map;
import java.util.Random;public class TestThreadMap {private static HashMap<Thread, Integer> map = new HashMap<Thread, Integer>();public static void main(String[] args) {//TestThreadMap testThreadMap = new TestThreadMap();for (int i = 0; i < 2; i++) {new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubint data = new Random().nextInt();map.put(Thread.currentThread(), data);new A().Get();new B().Get();}}){}.start();}}static class A{public void Get(){int data = map.get(Thread.currentThread());System.out.println("A from " + Thread.currentThread().getName() + " get data " + data);}}static class B{public void Get(){int data = map.get(Thread.currentThread());System.out.println("B from " + Thread.currentThread().getName() + " get data " + data);}}
}
ThreadLocal类似HashMap存储线程
import java.util.HashMap;
import java.util.Map;
import java.util.Random;public class TestThreadMap {private static HashMap<Thread, Integer> map = new HashMap<Thread, Integer>();public static void main(String[] args) {//TestThreadMap testThreadMap = new TestThreadMap();for (int i = 0; i < 2; i++) {new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubint data = new Random().nextInt();map.put(Thread.currentThread(), data);new A().Get();new B().Get();}}){}.start();}}static class A{public void Get(){int data = map.get(Thread.currentThread());System.out.println("A from " + Thread.currentThread().getName() + " get data " + data);}}static class B{public void Get(){int data = map.get(Thread.currentThread());System.out.println("B from " + Thread.currentThread().getName() + " get data " + data);}}
}
import java.util.Random;public class TestThreadlocal_2 {public static void main(String[] args) {// TODO Auto-generated method stubfor (int i = 0; i < 2; i++){new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubint data = new Random().nextInt();MyData.getInstance().setName("myData" + data);MyData.getInstance().setAge(data);new A().Get();new B().Get();}}){}.start();}}static class A{public void Get(){MyData data = MyData.getInstance();System.out.println("A from " + Thread.currentThread().getName() + " get data " + data.getName() + ", " + data.getAge());}}static class B{public void Get(){MyData data = MyData.getInstance();System.out.println("B from " + Thread.currentThread().getName() + " get data " + data.getName() + ", " + data.getAge());}}}class MyData{String name;int age;public static /*synchronized*/ MyData getInstance(){MyData instance = threadLocal.get();if (instance == null){  //不存在就创建一个与本线程有关的实例对象instance = new MyData();threadLocal.set(instance);}return instance;}//private static MyData instance = null;private static ThreadLocal<MyData> threadLocal = new ThreadLocal<MyData>();public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
线程池
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;public class TestThreadPool {public static void main(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(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubfor (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() {@Overridepublic void run() {// TODO Auto-generated method stubSystem.out.println("booming");}}, 6,TimeUnit.SECONDS); //多长时间后执行任务
*/        Executors.newScheduledThreadPool(3).scheduleAtFixedRate( //以固定频率执行任务new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubSystem.out.println("booming");}}, 6,  //初始时间2,  //间隔时间TimeUnit.SECONDS);}
}
锁Lock
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;import javax.xml.stream.events.StartDocument;public class TestLock {public static void main(String[] args) {// TODO Auto-generated method stubTestLock test = new TestLock();test.init();}void init() {final Outputer outputer = new Outputer();new Thread(new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){try {Thread.sleep(10);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}outputer.Output("wangyuyuyuyuyuyuyuyu");}}}){}.start();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){try {Thread.sleep(10);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}outputer.Output("zhouzhanzhaozhaozhaozhao");}}}){}.start();}class Outputer{Lock lock = new ReentrantLock(); //锁public void Output(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 exceptionlock.unlock();}}}}
读写锁ReadWriterLock
import java.util.HashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;public class TestReadWriterLock {HashMap<String, Object> mp = new HashMap<String, Object>();private ReadWriteLock rwl = new ReentrantReadWriteLock();public Object 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 clauserwl.writeLock().unlock();}rwl.readLock().lock();}} finally {rwl.readLock().unlock();}return value;}
}
信号灯Semaphere(控制当前运行的线程个数)
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.function.IntToDoubleFunction;public class TestSemaphere {public static void main(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(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {semaphore.acquire();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}System.out.println("线程" + Thread.currentThread().getName() + "进入");try {Thread.sleep((int)Math.random() * 10000);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}System.out.println("线程" + Thread.currentThread().getName() + "即将结束");semaphore.release();System.out.println("线程" + Thread.currentThread().getName() + "已结束");es.shutdown();}});}}}
数据交换Exchanger(当两个数据都到达后才能进行交换,否则阻塞)
import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class TestExchanger {public static void main(String[] args) {// TODO Auto-generated method stubExecutorService es = Executors.newCachedThreadPool();Exchanger<String> exchanger = new Exchanger<String>();es.execute(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {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 exceptione.printStackTrace();}}});es.execute(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {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 exceptione.printStackTrace();}}});}}
同步屏障CyclicBarrier(多个线程彼此等待,集合后再往后运行)
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class TestCyclicBarrier {//多个线程彼此等待,集合后再运行public static void main(String[] args) {// TODO Auto-generated method stub//创建线程池和CyclicBarrier,同时运行多个线程,调用awaitExecutorService es = Executors.newCachedThreadPool();final CyclicBarrier cb = new CyclicBarrier(3);for (int i = 0; i < 3; i++){es.execute(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {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 exceptione.printStackTrace();}}});}es.shutdown();}}
CountDownLatch(类似倒计时计数器,当计数减为0时,所有等待者才开始执行)
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class TestCountDownLatch {//类似倒计时计数器,当计数减为0的时候,所有等待者才开始执行public static void main(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(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {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 exceptione.printStackTrace();}}});}try {Thread.sleep(1000);System.out.println("裁判员发出指令");cdOrder.countDown();System.out.println("裁判员等待所有运动员到达终点");cdAnswer.await();System.out.println("裁判员公布成绩");} catch (Exception e) {// TODO: handle exception}}}
Callable和Future
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;public class TestCallableAndFuture {public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException{ExecutorService threadPool = Executors.newSingleThreadExecutor();Future future =threadPool.submit(new Callable<String>() {@Overridepublic String call() throws Exception {// TODO Auto-generated method stubThread.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<Integer> completionService = new ExecutorCompletionService<Integer>(threadPool2);for (int i = 1; i <= 10; i++){final int taskid = i;completionService.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {// TODO Auto-generated method stubtry {Thread.sleep(new Random().nextInt(5000));} catch (Exception e) {// TODO: handle exception}return taskid;}});}for (int i = 1; i <= 10; i++){System.out.println(completionService.take().get());}}}
阻塞队列ArrayBlockingQueue
import java.util.concurrent.ArrayBlockingQueue;public class TestArrayBlockingQueue {//阻塞队列当队列为空时take会阻塞,当队列满时put会阻塞//用两个阻塞队列模拟两个线程交替运行//两个阻塞队列大小均设置为1,其中一个放一个数据public static void main(String[] args) {// TODO Auto-generated method stubBusiness business = new Business();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubfor (int j = 1; j <= 10; j++) {business.Sub(j);}}}){}.start();for (int j = 1; j <= 10; j++) {business.Main(j);}}static class Business{ArrayBlockingQueue abq1 = new ArrayBlockingQueue(1);ArrayBlockingQueue abq2 = new ArrayBlockingQueue(1);public Business() {try {abq2.put(1);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}public void Sub(int j){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 exceptione.printStackTrace();}}public void Main(int j){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 exceptione.printStackTrace();}}}}
练习: 子线程运行10次,主线程运行20次,交替运行
public class prictice_1 {//子线程运行10次,主线程运行20次public static void main(String[] args) {// TODO Auto-generated method stubBusiness business = new Business();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubfor (int j = 1; j <= 10; j++) {business.Sub(j);}}}){}.start();for (int j = 1; j <= 10; j++) {business.Main(j);}}}
class Business{private boolean isSub = true;public synchronized void Sub(int j){while (!isSub) {try {this.wait();} catch (Exception e) {// TODO: handle exceptione.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(int j){while (isSub){try {this.wait();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}for (int i = 1; i <= 20; i++){System.out.println("main thread " + i + " of loop " + j);}isSub = true;this.notify();}
}

JAVA Java多线程与并发库相关推荐

  1. Java多线程与并发库高级应用--18_传智播客_张孝祥_java5阻塞队列的应用

    Java多线程与并发库高级应用--18_传智播客_张孝祥_java5阻塞队列的应用 原创:徐工 2018-5- 5 17.10 package cn.itcast.heima2; import jav ...

  2. Java多线程与并发库高级应用架构

    张孝祥_Java多线程与并发库高级应用 [视频介绍:] Java线程是一项非常基本和重要的技术,在偏底层和偏技术的Java程序中不可避免地要使用到Java线程技术,特别是android手机程序和游戏开 ...

  3. Java 从多线程到并发编程(五)—— 线程调度 优先级倒置(反转) 阻塞 死锁 suspend

    文章目录 前言 ´・ᴗ・` 线程调度策略 优先级倒置问题 优先级倒置解决方案 死锁 dead lock suspend 被阻塞的同时持有资源不放 是上述问题的诱因 总结 ´◡` 前言 ´・ᴗ・` 这一 ...

  4. Java多线程与并发库高级应用 学习笔记 1-9课

    来源XXX,免得打广告嫌疑. http://www.cnblogs.com/whgw/archive/2011/10/03/2198506.html 今天看了文章才发现创建线程最佳方式为实现Runna ...

  5. java定时器 并发_【java多线程与并发库】— 定时器的应用 | 学步园

    定时器的应用 1.  定时器主要涉及到两个类(java.util包中) @->public class Timer extendsObject (一种工具,线程用其安排以后在后台线程中执行的任务 ...

  6. Java面试——多线程高并发

    1.什么是线程?线程和进程有什么区别? 答:线程是程序执行的最小执行单位,进程是资源分配的最小单位:一个进程就是一个应用程序,系统会为该进程分配资源空间,当多用户并发请求的时候,为每个用户创建一个进程 ...

  7. java 多进程多线程_Java并发编程原理与实战三:多线程与多进程的联系以及上下文切换所导致资源浪费问题...

    一.进程 考虑一个场景:浏览器,网易云音乐以及notepad++ 三个软件只能顺序执行是怎样一种场景呢?另外,假如有两个程序A和B,程序A在执行到一半的过程中,需要读取大量的数据输入(I/O操作),而 ...

  8. 共同学习Java源代码-多线程与并发-FutureTask类(三)

    static final class WaitNode {         volatile Thread thread;         volatile WaitNode next;       ...

  9. Java多线程与线程并发库高级应用笔记

    以下内容是学习张老师Java多线程与线程并发库高级应用时所做的笔记,很有用 网络编辑器直接复制Word文档排版有点乱,提供原始文件下载 先看源文件概貌 张孝祥_Java多线程与并发库高级应用 [视频介 ...

最新文章

  1. 金蝶应收应付模块流程_金蝶KIS专业版应收应付的系统亮点功能
  2. 如何成为一个卓越的程序员
  3. Linux 下监控系统几个重要组件
  4. 使用Template时 typename 关键字的用法
  5. 简而言之,JUnit:测试结构
  6. php链接数据库实行增删查改_最方便最简单的php操作mysql的增删改查方法
  7. swagger访问开关配置
  8. 控制流图|圈复杂度|基本复杂度
  9. POJ2403 Hay Points
  10. tp5之允许跨域请求
  11. 论文笔记_S2D.73_ICCV2021_单目深度估计的可解释深度网络研究
  12. PHP操作文件的常用函数
  13. 电脑小技巧:怎么取消电脑开机密码
  14. 前端开发中spa的优缺点_使用单Spa开发和部署微前端
  15. JavaScript:实现PigeonHoleSort鸽巢排序算法(附完整源码)
  16. 第二章 从优化业务流程谈信息集成的必要性
  17. [小技巧] git: Your branch and 'origin/master' have diverged
  18. 鲁大师发布2022半年报手机UI排行榜,vivo OriginOS成为最流畅UI
  19. 精仿互站模板 友价源码商城T5内核二次开发运营版
  20. 测试 必用 工具(测试工具知多少)

热门文章

  1. 剑指offer之树的子结构
  2. LeetCode之Sqrt(x)
  3. Android安全与逆向之Dex动态加载
  4. Android之 ListView滑动时不加载图片
  5. chmod 777 修改权限
  6. 如何解决Maven依赖本地仓库eclipse报错的问题
  7. 【前端就业课 第一阶段】HTML5 零基础到实战(十)JavaScript基础一篇入门
  8. 微软官方pe工具_微软官方下载工具
  9. 如何把照片正面变成反面_没有锁边机如何做衣服(五种方法)
  10. 电脑用户名_仁霸下料优化软件如何找回密码、更换绑定电脑?