bjmashibing / juc Goto Github PK
View Code? Open in Web Editor NEW多线程与高并发
多线程与高并发
in c-020_01_interview NotifyFreeLock.java
ew Thread(() -> {
synchronized(lock) {
System.out.println("t2启动");
if(c.size() != 5) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("t2 结束");
//通知t1继续执行
lock.notify();
}
}, "t2").start();
public class T12_00_Exchanger_Not_Work {
private static Exchanger exchanger = new Exchanger<>();
public static void main(String[] args) {
char[] aI = "1234567".toCharArray();
char[] aC = "ABCDEFG".toCharArray();
new Thread(()->{
for(int i=0; i<aI.length;) {
try {
String t1 = exchanger.exchange("T1");
if ("T2".equals(t1)) {
System.out.print(aI[i]);
i++;
}
exchanger.exchange("T2");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(()->{
for(int i=0; i<aC.length;) {
try {
String t2 = exchanger.exchange("T2");
if ("T2".equals(t2)) {
System.out.print(aC[i]);
i++;
exchanger.exchange("T2");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
如下编写后exchanger can work
package com.mashibing.juc.c_026_00_interview.A1B2C3;
import java.util.concurrent.Exchanger;
public class T12_00_Exchanger {
private static Exchanger<String> exchanger = new Exchanger<>();
public static void main(String[] args) {
char[] aI = "1234567".toCharArray();
char[] aC = "ABCDEFG".toCharArray();
new Thread(()->{
String t = "T1";
for(int i=0; i<aI.length; i++) {
if ("T1".equals(t))
System.out.print(aI[i]);
else
i--;
try {
t = exchanger.exchange(t);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(()->{
String t = "T2";
for(int i=0; i<aC.length; i++) {
try {
t = exchanger.exchange(t);
} catch (InterruptedException e) {
e.printStackTrace();
}
if ("T1".equals(t))
System.out.print(aC[i]);
else
i--;
}
}).start();
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.