多线程2
等待唤醒机制:
--------------
public class Cook extends Thread{
@Override
public void run() {
while(true){
synchronized (Desk.lock){
if(Desk.count==0){
break;
}else{
if(Desk.flag==0){
System.out.println("厨师正在做包子");
Desk.flag=1;
Desk.lock.notify();
}else{
try {
Desk.lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}
}
}
---------------
package a03;
public class Desk {
/*
作用:控制生产者消费者执行
有食物:1, 没有:0
*/
public static int flag=0;
//总个数:
public static int count=10;
//锁
public static Object lock=new Object();
}
-----------
package a03;
public class Eater extends Thread{
/*
1.循环
2.同步代码块
3.判断是否到了末尾
*/
@Override
public void run() {
while (true){
synchronized (Desk.lock){
if(Desk.count==0){
break;
}else{
//判断有无食物
if(Desk.flag==0){
try {
Desk.lock.wait();//等待
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}else{
System.out.println("吃货吃了第"+Desk.count+"个章鱼小丸子");
Desk.lock.notify();//唤醒厨师继续做
Desk.count--;
Desk.flag=0;
}
}
}
}
}
}
--------------
package a03;
public class ThreadDemo1 {
public static void main(String[] args) {
/*
等待唤醒机制
*/
//创建线程对象
Cook c=new Cook();
Eater e=new Eater();
//设置线程名字
c.setName("厨师");
e.setName("吃货");
//开启线程
c.start();
e.start();
}
}
---------------------------------------
阻塞队列:
--------
package a04;
import java.util.concurrent.ArrayBlockingQueue;
public class Cook1 extends Thread {
ArrayBlockingQueue<String> queue;
public Cook1(ArrayBlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
while (true) {
//不断把面条添加到阻塞队列中
try {
queue.put("章鱼小丸子");//底层默认添加了锁
System.out.println("厨师做了一个章鱼小丸子");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
-------------
package a04;
import a03.Desk;
import java.util.concurrent.ArrayBlockingQueue;
public class Eater extends Thread{
/*
1.循环
2.同步代码块
3.判断是否到了末尾
*/
ArrayBlockingQueue<String> queue;
public Eater(ArrayBlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
while (true){
//不断把章鱼小丸子从到阻塞队列中拿出
try {
String food = queue.take();//底层默认添加了锁
System.out.println(food+"被吃了");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
-------------
package a04;
import a03.Cook;
import java.util.concurrent.ArrayBlockingQueue;
public class ThreadDemo {
/*
利用阻塞队列实现生产者消费者模式
*/
public static void main(String[] args) {
//创建阻塞队列
ArrayBlockingQueue<String> queue=new ArrayBlockingQueue<>(1);//要指定上限
Cook1 c=new Cook1(queue);
Eater e=new Eater(queue);
c.start();
e.start();
}
}
-------------
打印出现问题是因为输出语句在锁的外面;
-------------------------------
抢红包:
-------------
import java.util.Random;
public class MyThread10 extends Thread{
static double money=100;
static int count=3;
@Override
public void run(){
synchronized (MyThread10.class){
if(count==0){
System.out.println(this.getName()+"没抢到");
}else{
double prize;
if(count==1){
prize=money;
count--;
}else {
Random r=new Random();
prize=r.nextDouble(money-(count+1)*0.01)+0.01;
money-=prize;
count--;
}
System.out.println("抢到了"+prize+"元");
}
}
}
}
-----------------------------------------------------
import java.util.Random;
public class MyThread10 extends Thread{
static double money=100;
static int count=3;
@Override
public void run(){
synchronized (MyThread10.class){
if(count==0){
System.out.println(this.getName()+"没抢到");
}else{
double prize;
if(count==1){
prize=money;
count--;
}else {
Random r=new Random();
prize=r.nextDouble(money-(count+1)*0.01)+0.01;
money-=prize;
count--;
}
System.out.println("抢到了"+prize+"元");
}
}
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Hexo!