#java实现王道多线程情景的编写
生产者消费者
public class 生产者消费者 {
public static void main(String[] args) {
Semaphore full = new Semaphore(0);//表示 empty();
Semaphore empty = new Semaphore(3);//表示 empty();
Deque<Integer> dq = new LinkedList<>();
Object lock=new Object();
new Thread(new Runnable(){
//生产者代码
public void run(){
while(true){
try {
empty.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock){
dq.offerFirst(1);
System.out.println("放入了一个资源");
}
full.release();
}
}
}).start();
new Thread(new Runnable() {
//这里是消费者代码
@SneakyThrows
@Override
public void run() {
while (true){
try {
full.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock){
Thread.sleep(500);
Integer first = dq.pollFirst();//直接取,保证有;
System.out.println(first);
}
empty.release();
}
}
}).start();
}
}
多生产者多消费者
public class 多消费者模型 {
public static void main(String[] args) {
Semaphore plate = new Semaphore(1);
Semaphore orange=new Semaphore(0);
Semaphore apple=new Semaphore(0);
Thread dad = new Thread(new Runnable() {
//dad、、 往里面放橙子
@SneakyThrows
@Override
public void run() {
while(true){
Thread.sleep(300);
try {
plate.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
orange.release();//女儿爱吃橘子
}
}
});
Thread mom = new Thread(new Runnable() {
//mom 往里面放apple
@SneakyThrows
@Override
public void run() {
while(true){
Thread.sleep(1000);//苹果生产速度很慢
try {
plate.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
apple.release();//儿子爱吃苹果
}
}
});
Thread son = new Thread(new Runnable() {
//dad、、 往里面放橙子
@Override
public void run() {
while(true){
try {
apple.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("儿子吃到了一个苹果");
plate.release();
}
}
});
Thread daughter = new Thread(new Runnable() {
//dad、、 往里面放橙子
@Override
public void run() {
while (true){
try {
orange.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("女儿吃到了一个橘子");
plate.release();
}
}
});
dad.start();
mom.start();
son.start();
daughter.start();
}
}
读写分离,饥饿的情况
public class 读写锁公平写法 {
static int count=0;
public static void main(String[] args) {
Lock mu =new ReentrantLock();
Lock rw=new ReentrantLock();
new Thread(()->{
while(true){
try{
rw.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是写线程");
}catch (Exception e){
}
finally {
rw.unlock();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(()->{
while (true){
mu.lock();
count++;
if(count==1){
rw.lock();
}
mu.unlock();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我在读1");
mu.lock();
count--;
if(count==0){
rw.unlock();
}
mu.unlock();
}
}).start();
//读的线程是可以并发跑起来的;
new Thread(()->{
while (true){
mu.lock();
count++;
if(count==1){
rw.lock();
}
mu.unlock();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我在读2");
mu.lock();
count--;
if(count==0){
rw.unlock();
}
mu.unlock();
}
}).start();
}
}
读写分离 保护写不会被饥饿
public class 读写锁公平写法2 {
static int count=0;
public static void main(String[] args) {
Lock mu =new ReentrantLock();
Lock rw=new ReentrantLock();
Lock r3=new ReentrantLock();
new Thread(()->{
while (true){
try{
r3.lock();
mu.lock();
if(count==0){
rw.lock();
}
count++;
}catch (Exception e){}
finally {
mu.unlock();
r3.unlock();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我在读1");
try{
mu.lock();
count--;
if(count==0){
rw.unlock();
}
}catch (Exception e){}
finally {
mu.unlock();
}
}
},"读者1").start();
//读的线程是可以并发跑起来的;
new Thread(()->{
while (true){
try{
r3.lock();
mu.lock();
if(count==0){
rw.lock();
}
count++;
}catch (Exception e){}
finally {
mu.unlock();
r3.unlock();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我在读2");
try{
mu.lock();
count--;
if(count==0){
rw.unlock();
}
}catch (Exception e){}
finally {
mu.unlock();
}
}
},"读者2").start();
new Thread(()->{
while(true){
try{
r3.lock();
rw.lock();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是写线程");
}catch (Exception e){
}
finally {
rw.unlock();
r3.unlock();
}
}
}).start();
}
}