多线程
多线程学习,主要是刷题
gunsmoke
这个作者很懒,什么都没留下…
展开
-
用CountDownLatch模拟学生入校操作
想象场景学校开门了学生才能入校,当全部10名学生都入校后打印入校完毕,程序退出package study;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;public class Test { public static class myThread extends Thread { int sleepSeconds; CountDownLatch doorOpen; //原创 2020-05-26 10:29:03 · 150 阅读 · 0 评论 -
CountDownLatch的简单使用
个人理解就是当指定数量的线程全部执行完毕后再进行下一步动作package study;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;public class Test { public static class myThread extends Thread { int sleepSeconds; CountDownLatch countDownLatch; publi原创 2020-05-26 09:51:43 · 116 阅读 · 0 评论 -
统计多线程程序的耗时
package study;import java.util.concurrent.TimeUnit;public class Test { public static class myThread extends Thread { int sleepSeconds; public myThread(String name,int seconds) { this.setName(name); this.sleepSeconds = seconds; } @Overrid原创 2020-05-26 09:24:22 · 1551 阅读 · 0 评论 -
Semaphore的简单使用
package study;import java.util.concurrent.Semaphore;public class Test2 { static Semaphore semaphore = new Semaphore(2,true); public static void main(String[] args) throws InterruptedException { for(int index = 0; index < 10; index++) { new Th原创 2020-05-25 11:26:50 · 144 阅读 · 0 评论 -
让线程等待和唤醒的3种方式
使用Objectpackage study;public class Test { public static void main(String[] args) throws InterruptedException { Object lock = new Object(); Thread t1 = new Thread(() -> { System.out.println("进入线程t1"); synchronized (lock) { System.out.pr原创 2020-05-25 10:27:06 · 1287 阅读 · 0 评论 -
用Condition实现阻塞队列
package com.dq;import java.util.LinkedList;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class BlockingQueueDemo<E> { int size; // 队列...原创 2020-05-05 12:47:35 · 220 阅读 · 0 评论 -
利用Condition实现线程通信
package com.dq;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class ThreadTest { private static ReentrantLock lock = new ReentrantLock(); pr...原创 2020-05-04 12:48:21 · 132 阅读 · 0 评论 -
ReentrantLock之设置获取锁的超时时间
package com.dq;import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.ReentrantLock;public class ThreadTest { private static ReentrantLock lock = new ReentrantLock(true); pub...原创 2020-05-03 22:24:21 · 2897 阅读 · 0 评论 -
ReentrantLock公平锁演示代码
package com.dq;import java.util.concurrent.locks.ReentrantLock;public class ThreadTest { // 用true初始化锁就得到公平锁 private static ReentrantLock lock = new ReentrantLock(true); public static class T...原创 2020-05-03 21:23:35 · 269 阅读 · 0 评论 -
ReentrantLock实现数据一致性
package com.dq;import java.util.concurrent.locks.ReentrantLock;public class ThreadTest { private static int num = 0; private static ReentrantLock lock = new ReentrantLock(); public static cl...原创 2020-05-03 20:27:29 · 250 阅读 · 0 评论 -
jol查看对象内存布局
引入依赖 <dependency> <groupId>org.openjdk.jol</groupId> <artifactId>jol-core</artifactId> <version>0.9</version> &l...原创 2020-05-01 19:59:09 · 403 阅读 · 0 评论 -
CAS原则保持数据一致性
上代码public class Test { public static AtomicInteger count = new AtomicInteger(0); public static class T1 extends Thread { @Override public void run() { for(int index = 0; index < 10000; in...原创 2020-05-01 19:35:01 · 767 阅读 · 0 评论 -
volatile关键字初体验
直接上代码public class Test { public static boolean flag = true; public static class T1 extends Thread { @Override public void run() { System.out.println("线程运行中"); while(flag) { ; } ...原创 2020-04-30 21:30:12 · 80 阅读 · 0 评论 -
用线程组体验停止线程
package com.dq;public class ThreadTest { public static class Task implements Runnable { @Override public void run() { Thread currentThread = Thread.currentThread(); while (true) ...原创 2020-03-08 21:13:06 · 143 阅读 · 0 评论 -
动画演示java并发编程
下载可执行jar文件http://sourceforge.net/projects/javaconcurrenta/,进入后点击download,下载后得到javaConcurrentAnimated.jar文件运行java -jar javaConcurrentAnimated.jar确实很牛逼...原创 2020-03-07 16:18:32 · 242 阅读 · 0 评论 -
死锁的示例代码
package com.dq;public class ThreadTest implements Runnable{ // 2个对象代表2把锁 Object obj1 = new Object(); Object obj2 = new Object(); @Override public void run() { if(Thread.currentThread().g...原创 2020-03-07 15:01:13 · 257 阅读 · 0 评论 -
主线程无法catch子线程的异常
上代码package com.dq;public class ThreadTest implements Runnable{ @Override public void run() { throw new RuntimeException(); } public static void main(String[] args) { ThreadTest task ...原创 2020-03-01 19:37:16 · 674 阅读 · 1 评论 -
代码演示线程的wait,notify方法
package com.dq;public class ThreadTest implements Runnable{ public static Object lock = new Object(); @Override public void run() { System.out.println("进入线程执行体,当前线程名: " + Thread.currentThrea...原创 2020-02-27 18:49:47 · 107 阅读 · 0 评论 -
代码演示线程的TIMED_WAITING,BLOCKED,WAITING状态
package com.dq;public class ThreadTest implements Runnable{ @Override public void run() { // 线程1先进这个方法,当线程1睡眠时,线程2没有得到锁,于是它的状态为BLOCKED synchronized (this) { System.out.println(Thread....原创 2020-02-24 21:56:35 · 406 阅读 · 0 评论 -
代码演示线程的NEW,RUNNABLE,TERMINATED状态
package com.dq;public class ThreadTest extends Thread{ @Override public void run() { for(int i = 0; i < 10; i++) { // 线程在运行中状态是RUNNABLE,不是想象中的RUNNING,以下代码就是证明 System.out.println("线程...原创 2020-02-24 20:24:04 · 1047 阅读 · 0 评论 -
线程状态图
原创 2020-02-24 19:56:23 · 93 阅读 · 0 评论 -
线程方法中有异常时线程的停止
package com.dq;public class ThreadTest { public static void main(String[] args) { Runnable task = new Task(); Thread thread = new Thread(task); thread.start(); try { Thread.sleep(100)...原创 2020-02-20 19:05:50 · 994 阅读 · 1 评论 -
线程的停止操作1
上代码package com.gunsmoke;public class TheadTest implements Runnable{ @Override public void run() { System.out.println("run方法start"); for(int index = 0; index < 100000; index++) { S...原创 2020-02-18 13:44:56 · 106 阅读 · 0 评论 -
线程池Executors使用初步
直接上代码package com.gunsmoke;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ThreadTest { public static void main(String[] args) { ExecutorService p...原创 2020-02-17 20:49:50 · 71 阅读 · 0 评论 -
创建线程时继承Thread类同时实现Runnable接口会怎样
直接上代码package com.dq;public class ThreadTest { public static void main(String[] args) { new Thread( new Runnable() { public void run() { System.out.println("传入runnable对象...原创 2020-02-17 19:55:48 · 320 阅读 · 0 评论 -
写2个线程,其中一个线程打印1~52,另一个线程打印A~Z,打印顺序是12A34B56C...5152Z
charThread.javapackage thread8;public class CharThread extends Thread{ private Print print; public CharThread(Print print) { super(); this.print = print; } @Override public void run() ...原创 2020-02-12 20:13:00 · 286 阅读 · 0 评论