package demo;
/*定义商品*/
public class Goods {
public final String name;
public final int price;
public final int id;
public Goods(String name, int price, int id){
this.name = name;
/*类型*/
this.price = price;
/*价格*/
this.id = id;
/*商品序列号*/
}
@Override
public String toString(){
return "name: " + name + ", price:"+ price + ", id: " + id;
}
}
package demo;
import java.util.Random;
/*使用线程对象,一个缓存位置,一个生产者,一个消费者,无限生产商品消费商品*/
public class ProducterComsumerDemo1 {
/*定义一个商品缓存位置*/
private volatile Goods goods;
/*定义一个对象作为锁,不使用goods作为锁是因为生产者每次会产生一个新的对象*/
private Object obj = new Object();
/*isFull == true 生产者线程休息,消费者线程消费
*isFull == false 消费者线程休息,生产者线程生产*/
private volatile Boolean isFull = false;
/*商品的id编号,生产者制造的每个商品的id都不一样,每生产一个id自增1*/
private int id = 1;
/*随机产生一个sleep时间*/
private Random rnd = new Random();
/*=================定义消费者线程==================*/
public class ComsumeThread implements Runnable{
@Override
public void run(){
try{
while(true){
/*获取obj对象的锁, id 和 isFull 的操作都在同步代码块中*/
synchronized(obj){
if(!isFull){
/*wait方法使当前线程阻塞,并释放锁*/
obj.wait();
}
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(250));
/*模拟消费商品*/
System.out.println(goods);
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(250));
isFull = false;
/*唤醒阻塞obj上的生产者线程*/
obj.notify();
}
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(250));
}
}
catch (InterruptedException e){
/*什么都不做*/
}
}
}
/*=================定义生产者线程==================*/
public class ProductThread implements Runnable{
@Override
public void run(){
try {
while(true){
synchronized(obj){
if(isFull){
obj.wait();
}
Thread.sleep(rnd.nextint(500));
/*如果id为偶数,生产价格为2的产品A
*如果id为奇数,生产价格为1的产品B*/
if(id % 2 == 0){
goods = new Goods("A", 2, id);
} else{
goods = new Goods("B", 1, id);
}
Thread.sleep(rnd.nextint(250));
id++;
isFull = true;
/*唤醒阻塞的消费者线程*/
obj.notify();
}
}
}
catch (InterruptedException e) {
/*什么都不做*/
}
}
}
public static void main(String[] args) throws InterruptedException{
ProducterComsumerDemo1 pcd = new ProducterComsumerDemo1();
Runnable c = pcd.new ComsumeThread();
Runnable p = pcd.new ProductThread();
new Thread(p).start();
new Thread(c).start();
}
}
name: B, price:1, id: 1 name: A, price:2, id: 2 name: B, price:1, id: 3 name: A, price:2, id: 4 name: B, price:1, id: 5 name: A, price:2, id: 6 name: B, price:1, id: 7 name: A, price:2, id: 8 name: B, price:1, id: 9 name: A, price:2, id: 10 name: B, price:1, id: 11 name: A, price:2, id: 12 name: B, price:1, id: 13 ……
public static void main(String[] args) throws InterruptedException{
ProducterComsumerDemo1 pcd = new ProducterComsumerDemo1();
Runnable c = pcd.new ComsumeThread();
Runnable p = pcd.new ProductThread();
new Thread(c).start();
new Thread(p).start();
new Thread(c).start();
new Thread(c).start();
}
name: B, price:1, id: 1 name: A, price:2, id: 2 name: A, price:2, id: 2 name: B, price:1, id: 3 name: B, price:1, id: 3 name: A, price:2, id: 4 name: A, price:2, id: 4 name: B, price:1, id: 5 name: B, price:1, id: 5 name: A, price:2, id: 6 ……
package demo;
import java.util.Random;
/*使用线程对象,一个缓存位置,一个生产者,一个消费者,无限生产商品消费商品*/
public class ProducterComsumerDemo1 {
/*定义一个商品缓存位置*/
private volatile Goods goods;
/*定义一个对象作为锁,不使用goods作为锁是因为生产者每次会产生一个新的对象*/
private Object obj = new Object();
/*isFull == true 生产者线程休息,消费者线程消费
*isFull == false 消费者线程消费,生产者线程生产*/
private volatile Boolean isFull = false;
/*商品的id编号,生产者制造的每个商品的id都不一样,每生产一个id自增1*/
private int id = 1;
/*随机产生一个sleep时间*/
private Random rnd = new Random();
/*=================定义消费者线程==================*/
public class ComsumeThread implements Runnable{
@Override
public void run(){
try{
while(true){
/*获取obj对象的锁, id 和 isFull 的操作都在同步代码块中*/
synchronized(obj){
while(!isFull){
/*wait方法使当前线程阻塞,并释放锁*/
obj.wait();
}
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(250));
/*模拟消费商品*/
System.out.println(goods);
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(250));
isFull = false;
/*唤醒阻塞obj上的生产者线程*/
obj.notifyAll();
}
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(250));
}
}
catch (InterruptedException e){
/*我就是任性,这里什么都不做*/
}
}
}
/*=================定义生产者线程==================*/
public class ProductThread implements Runnable{
@Override
public void run(){
try {
while(true){
synchronized(obj){
while(isFull){
obj.wait();
}
Thread.sleep(rnd.nextint(500));
/*如果id为偶数,生产价格为2的产品A
如果id为奇数,生产价格为1的产品B*/
if(id % 2 == 0){
goods = new Goods("A", 2, id);
} else{
goods = new Goods("B", 1, id);
}
Thread.sleep(rnd.nextint(250));
id++;
isFull = true;
/*唤醒阻塞的消费者线程*/
obj.notifyAll();
}
}
}
catch (InterruptedException e) {
/*我就是任性,这里什么都不做*/
}
}
}
public static void main(String[] args) throws InterruptedException{
ProducterComsumerDemo1 pcd = new ProducterComsumerDemo1();
Runnable c = pcd.new ComsumeThread();
Runnable p = pcd.new ProductThread();
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
new Thread(c).start();
new Thread(c).start();
new Thread(c).start();
}
}
package demo;
import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*使用线程对象,多个缓存位置(有界),多生产者,多消费者,无限循环模式*/
public class ProducterComsumerDemo2 {
/*最大缓存商品数*/
private final int MAX_SLOT = 2;
/*定义缓存商品的容器*/
private LinkedList<Goods> queue = new LinkedList<Goods>();
/*定义线程锁和锁对应的阻塞队列*/
private Lock lock = new ReentrantLock();
private Condition full = lock.newCondition();
private Condition empty = lock.newCondition();
/*商品的id编号,生产者制造的每个商品的id都不一样,每生产一个id自增1*/
private int id = 1;
/*随机产生一个sleep时间*/
private Random rnd = new Random();
/*=================定义消费者线程==================*/
public class ComsumeThread implements Runnable{
@Override
public void run(){
while(true){
/*加锁,queue的出列操作都在同步代码块中*/
lock.lock();
try {
while(queue.isEmpty()){
System.out.println("queue is empty");
empty.await();
}
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(200));
/*模拟消费商品*/
Goods goods = queue.remove();
System.out.println(goods);
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(200));
/*唤醒阻塞的生产者线程*/
full.signal();
}
catch (InterruptedException e) {
/*什么都不做*/
}
finally{
lock.unlock();
}
/*释放锁后随机延时一段时间*/
try {
Thread.sleep(rnd.nextint(200));
}
catch (InterruptedException e) {
/*什么都不做*/
}
}
}
}
/*=================定义生产者线程==================*/
public class ProductThread implements Runnable{
@Override
public void run(){
while(true){
/*加锁,queue的入列操作,id操作都在同步代码块中*/
lock.lock();
try{
while(queue.size() == MAX_SLOT){
System.out.println("queue is full");
full.await();
}
Thread.sleep(rnd.nextint(200));
Goods goods = null;
/*根据序号产生不同的商品*/
switch(id%3){
case 0 : goods = new Goods("A", 1, id);
break;
case 1 : goods = new Goods("B", 2, id);
break;
case 2 : goods = new Goods("C", 3, id);
break;
}
Thread.sleep(rnd.nextint(200));
queue.add(goods);
id++;
/*唤醒阻塞的消费者线程*/
empty.signal();
}
catch(InterruptedException e){
/*什么都不做*/
}
finally{
lock.unlock();
}
/*释放锁后随机延时一段时间*/
try {
Thread.sleep(rnd.nextint(100));
}
catch (InterruptedException e) {
/*什么都不做*/
}
}
}
}
/*=================main==================*/
public static void main(String[] args) throws InterruptedException{
ProducterComsumerDemo2 pcd = new ProducterComsumerDemo2();
Runnable c = pcd.new ComsumeThread();
Runnable p = pcd.new ProductThread();
/*两个生产者线程,两个消费者线程*/
new Thread(p).start();
new Thread(p).start();
new Thread(c).start();
new Thread(c).start();
}
}
queue is empty queue is empty name: B, price:2, id: 1 name: C, price:3, id: 2 name: A, price:1, id: 3 queue is full name: B, price:2, id: 4 name: C, price:3, id: 5 queue is full name: A, price:1, id: 6 name: B, price:2, id: 7 name: C, price:3, id: 8 name: A, price:1, id: 9 name: B, price:2, id: 10 name: C, price:3, id: 11 name: A, price:1, id: 12 name: B, price:2, id: 13 name: C, price:3, id: 14 ……
package demo;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
/*使用线程对象,多个缓存位置(有界),多生产者,多消费者,无限循环模式*/
public class ProducterComsumerDemo4 {
/*最大缓存商品数*/
private final int MAX_SLOT = 3;
/*定义缓存商品的容器*/
private LinkedBlockingQueue<Goods> queue = new LinkedBlockingQueue<Goods>(MAX_SLOT);
/*商品的id编号,生产者制造的每个商品的id都不一样,每生产一个id自增1*/
private AtomicInteger id = new AtomicInteger(1);
/*随机产生一个sleep时间*/
private Random rnd = new Random();
/*=================定义消费者线程==================*/
public class ComsumeThread implements Runnable{
@Override
public void run(){
while(true){
try {
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(200));
/*模拟消费商品*/
Goods goods = queue.take();
System.out.println(goods);
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(200));
}
catch (InterruptedException e) {
/*什么都不做*/
}
}
}
}
/*=================定义生产者线程==================*/
public class ProductThread implements Runnable{
@Override
public void run(){
while(true){
try{
int x = id.getAndIncrement();
Goods goods = null;
Thread.sleep(rnd.nextint(200));
/*根据序号产生不同的商品*/
switch(x%3){
case 0 : goods = new Goods("A", 1, x);
break;
case 1 : goods = new Goods("B", 2, x);
break;
case 2 : goods = new Goods("C", 3, x);
break;
}
Thread.sleep(rnd.nextint(200));
queue.put(goods);
Thread.sleep(rnd.nextint(100));
}
catch(InterruptedException e){
/*什么都不做*/
}
}
}
}
/*=================main==================*/
public static void main(String[] args) throws InterruptedException{
ProducterComsumerDemo4 pcd = new ProducterComsumerDemo4();
Runnable c = pcd.new ComsumeThread();
Runnable p = pcd.new ProductThread();
/*定义线程池*/
ExecutorService es = Executors.newCachedThreadPool();
/*三个生产者线程,两个消费者线程*/
es.execute(p);
es.execute(p);
es.execute(p);
es.execute(c);
es.execute(c);
es.shutdown();
}
}
/*需要生产的总商品数*/ private final int TOTAL_NUM = 30; /*已产生的数量*/ private volatile int productNum = 0; /*已消耗的商品数*/ private volatile int comsumedNum = 0;
package demo;
import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*使用线程对象,多个缓存位置(有界),多生产者,多消费者, 有限商品个数*/
public class ProducterComsumerDemo3 {
/*需要生产的总商品数*/
private final int TOTAL_NUM = 30;
/*已产生的数量*/
private volatile int productNum = 0;
/*已消耗的商品数*/
private volatile int comsumedNum = 0;
/*最大缓存商品数*/
private final int MAX_SLOT = 2;
/*定义线程公用的锁和条件*/
private Lock lock = new ReentrantLock();
private Condition full = lock.newCondition();
private Condition empty = lock.newCondition();
/*定义缓存商品的容器*/
private LinkedList<Goods> queue = new LinkedList<Goods>();
/*商品的id编号,生产者制造的每个商品的id都不一样,每生产一个id自增1*/
private int id = 1;
/*随机产生一个sleep时间*/
private Random rnd = new Random();
/*=================定义消费者线程==================*/
public class ComsumeThread implements Runnable{
@Override
public void run(){
while(true){
/*加锁, id、comsumedNum 操作都在同步代码块中*/
lock.lock();
try {
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(250));
if(comsumedNum < TOTAL_NUM){
comsumedNum++;
} else{
/*这里会自动执行finally的语句,释放锁*/
break;
}
while(queue.isEmpty()){
System.out.println("queue is empty");
empty.await();
}
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(250));
/*模拟消费商品*/
Goods goods = queue.remove();
System.out.println(goods);
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(250));
/*唤醒阻塞的生产者线程*/
full.signal();
}
catch (InterruptedException e) {
}
finally{
lock.unlock();
}
/*释放锁后,随机延时一段时间*/
try {
Thread.sleep(rnd.nextint(250));
}
catch (InterruptedException e) {
}
}
System.out.println(
"customer "
+ Thread.currentThread().getName()
+ " is over");
}
}
/*=================定义生产者线程==================*/
public class ProductThread implements Runnable{
@Override
public void run(){
while(true){
lock.lock();
try{
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(250));
if(productNum < TOTAL_NUM){
productNum++;
} else{
/*这里会自动执行finally的语句,释放锁*/
break;
}
Thread.sleep(rnd.nextint(250));
while(queue.size() == MAX_SLOT){
System.out.println("queue is full");
full.await();
}
Thread.sleep(rnd.nextint(250));
Goods goods = null;
/*根据序号产生不同的商品*/
switch(id%3){
case 0 : goods = new Goods("A", 1, id);
break;
case 1 : goods = new Goods("B", 2, id);
break;
case 2 : goods = new Goods("C", 3, id);
break;
}
queue.add(goods);
id++;
/*唤醒阻塞的消费者线程*/
empty.signal();
}
catch(InterruptedException e){
}
finally{
lock.unlock();
}
/*释放锁后,随机延时一段时间*/
try {
Thread.sleep(rnd.nextint(250));
}
catch (InterruptedException e) {
/*什么都不做*/
}
}
System.out.println(
"producter "
+ Thread.currentThread().getName()
+ " is over");
}
}
/*=================main==================*/
public static void main(String[] args) throws InterruptedException{
ProducterComsumerDemo3 pcd = new ProducterComsumerDemo3();
ComsumeThread c = pcd.new ComsumeThread();
ProductThread p = pcd.new ProductThread();
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
new Thread(c).start();
new Thread(c).start();
new Thread(c).start();
System.out.println("main Thread is over");
}
}
package demo;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
/*使用线程池,多个缓存位置(有界),多生产者,多消费者, 有限商品个数*/
public class LinkedBlockingQueueDemo {
/*需要生产的总商品数*/
private final int TOTAL_NUM = 20;
/*已产生商品的数量*/
volatile AtomicInteger productNum = new AtomicInteger(0);
/*已消耗的商品数*/
volatile AtomicInteger comsumedNum = new AtomicInteger(0);
/*最大缓存商品数*/
private final int MAX_SLOT = 5;
/*同步阻塞队列,队列容量为MAX_SLOT*/
private LinkedBlockingQueue<Goods> lbq = new LinkedBlockingQueue<Goods>(MAX_SLOT);
/*随机数*/
private Random rnd = new Random();
/*pn表示产品的编号,产品编号从1开始*/
private volatile AtomicInteger pn = new AtomicInteger(1);
/*=================定义消费者线程==================*/
public class CustomerThread implements Runnable{
@Override
public void run(){
while(comsumedNum.getAndIncrement() < TOTAL_NUM){
try{
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(500));
/*从队列中取出商品,队列空时发生阻塞*/
Goods goods = lbq.take();
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(500));
/*模拟消耗商品*/
System.out.println(goods);
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(500));
}
catch(InterruptedException e){
}
}
System.out.println(
"customer "
+ Thread.currentThread().getName()
+ " is over");
}
}
/*=================定义生产者线程==================*/
public class ProducerThread implements Runnable{
@Override
public void run(){
while(productNum.getAndIncrement() < TOTAL_NUM){
try {
int x = pn.getAndIncrement();
Goods goods = null;
/*根据序号产生不同的商品*/
switch(x%3){
case 0 : goods = new Goods("A", 1, x);
break;
case 1 : goods = new Goods("B", 2, x);
break;
case 2 : goods = new Goods("C", 3, x);
break;
}
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(500));
/*产生的新产品入列,队列满时发生阻塞*/
lbq.put(goods);
/*随机延时一段时间*/
Thread.sleep(rnd.nextint(500));
}
catch (InterruptedException e1) {
/*什么都不做*/
}
}
System.out.println(
"producter "
+ Thread.currentThread().getName()
+ " is over ");
}
}
/*=================main==================*/
public static void main(String[] args){
LinkedBlockingQueueDemo lbqd = new LinkedBlockingQueueDemo();
Runnable c = lbqd.new CustomerThread();
Runnable p = lbqd.new ProducerThread();
ExecutorService es = Executors.newCachedThreadPool();
es.execute(c);
es.execute(c);
es.execute(c);
es.execute(p);
es.execute(p);
es.execute(p);
es.shutdown();
System.out.println("main Thread is over");
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有