标签:container 消费者 生产者 shashlik new SynContainer public Shashlik
管程法【缓冲区法】
public class BufferPCTest_Shashlik {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Thread(new C(container)).start();
new Thread(new P(container)).start();
}
/*消费者*/
static class C implements Runnable{
private SynContainer container;
public C(SynContainer container){
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 200; i++) {
try {
System.out.println("吃羊肉串"+container.c().id);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*生产者*/
static class P implements Runnable{
private SynContainer container;
public P(SynContainer container){
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 200; i++) {
try {
container.p(new Shashlik(i));
System.out.println("烤羊肉串"+i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*盛放羊肉串的容器=>业务逻辑*/
static class SynContainer {
int counter = 0;
Shashlik[] container = new Shashlik[200];
/*烤羊肉串*/
public synchronized void p(Shashlik shashlik) throws InterruptedException {
if(counter == container.length){
this.wait();
}
counter++;
container[counter] = shashlik;
this.notifyAll();
}
/*吃羊肉串*/
public synchronized Shashlik c() throws InterruptedException {
if(counter == 0){
this.wait();
}
Shashlik shashlik = container[counter];
counter--;
this.notifyAll();;
return shashlik;
}
}
/*羊肉串*/
static class Shashlik{
int id;
public Shashlik(int id){
this.id = id;
}
}
}
标志位法
public static void main(String[] args) throws InterruptedException {
Shashlik shashlik = new Shashlik();
new Thread(new P(shashlik)).start();
new Thread(new C(shashlik)).start();
}
}
/*生产者*/
class P implements Runnable {
Shashlik shashlik;
public P(Shashlik shashlik) {
this.shashlik = shashlik;
}
@Override
public void run() {
for (int i = 1; i <= 200; i++) {
this.shashlik.p(i);
}
}
}
/*消费者*/
class C implements Runnable {
Shashlik shashlik;
public C(Shashlik shashlik) {
this.shashlik = shashlik;
}
@Override
public void run() {
for (int i = 1; i <= 200; i++) {
this.shashlik.c(i);
}
}
}
/*羊肉串 业务逻辑*/
class Shashlik {
boolean flag = true;
public synchronized void p(int num) {
try {
if (!flag) {
this.wait();
}
/*烤羊肉串要等哦*/
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println("烤羊肉串" + num);
this.notifyAll();
this.flag = !this.flag;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/*吃羊肉串*/
public synchronized void c(int num) {
if (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("吃羊肉串" + num);
this.notifyAll();
this.flag = !this.flag;
}
}
标签:container,消费者,生产者,shashlik,new,SynContainer,public,Shashlik 来源: https://www.cnblogs.com/haveanicedayfh/p/16466552.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。