java应用技术 1(2)

java应用技术
1(2)
1.生产者和消费者
案例:
package cn.java.java1;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class LockPSTheadsDemo1 {
public static void main(String[] args){
prodution4 pt = new prodution4();
produter4 pr  =new produter4(pt);
saler4 s = new saler4(pt);
Thread th1 = new Thread(pr);
Thread th2 = new Thread(s);
Thread th3 = new Thread(pr);
Thread th4 = new Thread(s);
th1.setName("叶良辰:");
th2.setName("叶良辰:");
th3.setName("叶良辰:");
th4.setName("叶良辰:");
th1.start();
th2.start();
th3.start();
th4.start();
}
}


class prodution4{
private String name;
private int i;
private boolean flag = false; 
private Lock lock = new ReentrantLock();
private Condition c_p = lock.newCondition();
private Condition c_s = lock.newCondition();
public prodution4() {
}
public prodution4(String name, int i,boolean flag) {
this.name = name;
this.i = i;
this.flag = false;
}

public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public void produtting(){
lock.lock();
while(this.isFlag()){
try {
c_p.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.setI(i+1);
this.setName("赵日天");
System.out.println(Thread.currentThread().getName()+"生产"+this.getName()+this.getI()+"号\n");
this.setFlag(true);
c_s.signal();
lock.unlock();
}
public void salseing(){
lock.lock();
while(!this.isFlag()){
try {
c_s.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"销售"+this.getName()+this.getI()+"号\n");
this.setFlag(false);
c_p.signal();
lock.unlock();
}
}
class produter4 implements Runnable{
private prodution4 p = null;
public produter4(){
}
public produter4(prodution4 p) {
this.p = p;
}

public prodution4 getP() {
return p;
}
public void setP(prodution4 p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub


for(int a = 0;a<100;a++){
p.produtting();


}


}

}
class saler4 implements Runnable{
private prodution4 p = null;
public saler4(){
}
public saler4(prodution4 p) {
this.p = p;
}

public prodution4 getP() {
return p;
}
public void setP(prodution4 p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int a = 0;a<100;a++){
p.salseing();

}
}

}
2.用同步代码块实现生产者和消费者
案例:
package cn.java.java1;
public class PSTheadsDemo {
public static void main(String[] args) {
Production1 p = new Production1();
producter1 pr1 = new producter1(p);
saler1 s1 = new saler1(p);
producter1 pr2 = new producter1(p);
saler1 s2 = new saler1(p);
Thread th1 = new Thread(pr1);
Thread th2 = new Thread(pr2);
Thread th3 = new Thread(s1);
Thread th4 = new Thread(s2);
th1.setName("生产窗口1:"+"\n");
th2.setName("生产窗口2:"+"\n");
th3.setName("出售窗口1:"+"\n");
th4.setName("出售窗口2:"+"\n");
th1.start();
th2.start();
th3.start();
th4.start();
}
}


class Production1 {
private String name;
private int i;
private boolean flag = false;


public Production1() {
}


public Production1(boolean flag) {
this.flag = flag;
}


public Production1(String name, int i) {
this.name = name;
this.i = i;
}


public boolean isFlag() {
return flag;
}


public void setFlag(boolean flag) {
this.flag = flag;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public int getI() {
return i;
}


public void setI(int i) {
this.i = i;
}


public synchronized void productting() {
for (int c = 0; c < 100; c++) {
while (this.isFlag()) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
i++;
this.setName("赵日天");
System.out.println(Thread.currentThread().getName() + "生产"
+ this.getName() + this.getI() + "号"+"\n");
this.notifyAll();
this.setFlag(true);
}
}


public synchronized void salerring() {
for (int c = 0; c < 100; c++) {
while (!this.isFlag()) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "出售"
+ this.getName() + this.getI() + "号"+"\n");
this.notifyAll();
this.setFlag(false);
}
}
}


class producter1 implements Runnable {
private Production1 p = null;


public producter1() {
}


public producter1(Production1 p) {
this.p = p;
}


public Production1 getP() {
return p;
}


public void setP(Production1 p) {
this.p = p;
}


@Override
public void run() {
// TODO Auto-generated method stub
p.productting();
}


}


class saler1 implements Runnable {
private Production1 p = null;


public saler1() {
}


public saler1(Production1 p) {
this.p = p;
}


public Production1 getP() {
return p;
}


public void setP(Production1 p) {
this.p = p;
}


@Override
public void run() {
// TODO Auto-generated method stub
p.salerring();
}


}
3.监视器实现生产者和消费者
案例:
package cn.java.java1;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class LockPSTheadsDemo1 {
public static void main(String[] args){
prodution4 pt = new prodution4();
produter4 pr  =new produter4(pt);
saler4 s = new saler4(pt);
Thread th1 = new Thread(pr);
Thread th2 = new Thread(s);
Thread th3 = new Thread(pr);
Thread th4 = new Thread(s);
th1.setName("叶良辰:");
th2.setName("叶良辰:");
th3.setName("叶良辰:");
th4.setName("叶良辰:");
th1.start();
th2.start();
th3.start();
th4.start();
}
}


class prodution4{
private String name;
private int i;
private boolean flag = false; 
private Lock lock = new ReentrantLock();
private Condition c_p = lock.newCondition();
private Condition c_s = lock.newCondition();
public prodution4() {
}
public prodution4(String name, int i,boolean flag) {
this.name = name;
this.i = i;
this.flag = false;
}

public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public void produtting(){
lock.lock();
while(this.isFlag()){
try {
c_p.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.setI(i+1);
this.setName("赵日天");
System.out.println(Thread.currentThread().getName()+"生产"+this.getName()+this.getI()+"号\n");
this.setFlag(true);
c_s.signal();
lock.unlock();
}
public void salseing(){
lock.lock();
while(!this.isFlag()){
try {
c_s.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"销售"+this.getName()+this.getI()+"号\n");
this.setFlag(false);
c_p.signal();
lock.unlock();
}
}
class produter4 implements Runnable{
private prodution4 p = null;
public produter4(){
}
public produter4(prodution4 p) {
this.p = p;
}

public prodution4 getP() {
return p;
}
public void setP(prodution4 p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub


for(int a = 0;a<100;a++){
p.produtting();


}


}

}
class saler4 implements Runnable{
private prodution4 p = null;
public saler4(){
}
public saler4(prodution4 p) {
this.p = p;
}

public prodution4 getP() {
return p;
}
public void setP(prodution4 p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int a = 0;a<100;a++){
p.salseing();

}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值