public class Info { private String name = "洗衣工";
private String content = "洗衣服";
private boolean flag = false; public Info() {
} public synchronized void set(String name, String content) {
if (!flag) {
try {
super.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Info.class.getName()).log(Level.SEVERE, null, ex);
}
}
this.setName(name);
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(Info.class.getName()).log(Level.SEVERE, null, ex);
}
this.setContent(content);
flag = false;
super.notify();
} public synchronized void get() {
if (flag) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Info.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println(this.getName() + ":" + this.getContent());
flag = true;
super.notify();
}
//get和set方法
}public class Consumer implements Runnable { private Info info; public Consumer(Info info) {
this.info = info;
} @Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
this.info.get();
}
}
}public class Producer implements Runnable { private Info info; public Producer(Info info) {
this.info = info; } public void run() {
boolean flag = false;
for (int i = 0; i < 50; i++) {
if (flag) {
this.info.set("洗衣工", "洗衣服");
flag = false;
} else {
this.info.set("晾衣工", "晾衣服");
flag = true;
}
}
}
}分别启动一个线程是这样
洗衣工:洗衣服
晾衣工:晾衣服
洗衣工:洗衣服
晾衣工:晾衣服
洗衣工:洗衣服
晾衣工:晾衣服题目要求是每个分别启动2线程 完事就乱了 就不是 洗完一件拿走一件了 应该怎么解决?题目要求
1. 洗衣工2人(对应2个线程)
2. 晾衣工2人
3. 共50件衣服
4. 洗衣工每2秒洗1件衣服
5. 晾衣工每1秒晾1件衣服
private String content = "洗衣服";
private boolean flag = false; public Info() {
} public synchronized void set(String name, String content) {
if (!flag) {
try {
super.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Info.class.getName()).log(Level.SEVERE, null, ex);
}
}
this.setName(name);
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(Info.class.getName()).log(Level.SEVERE, null, ex);
}
this.setContent(content);
flag = false;
super.notify();
} public synchronized void get() {
if (flag) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Info.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println(this.getName() + ":" + this.getContent());
flag = true;
super.notify();
}
//get和set方法
}public class Consumer implements Runnable { private Info info; public Consumer(Info info) {
this.info = info;
} @Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
this.info.get();
}
}
}public class Producer implements Runnable { private Info info; public Producer(Info info) {
this.info = info; } public void run() {
boolean flag = false;
for (int i = 0; i < 50; i++) {
if (flag) {
this.info.set("洗衣工", "洗衣服");
flag = false;
} else {
this.info.set("晾衣工", "晾衣服");
flag = true;
}
}
}
}分别启动一个线程是这样
洗衣工:洗衣服
晾衣工:晾衣服
洗衣工:洗衣服
晾衣工:晾衣服
洗衣工:洗衣服
晾衣工:晾衣服题目要求是每个分别启动2线程 完事就乱了 就不是 洗完一件拿走一件了 应该怎么解决?题目要求
1. 洗衣工2人(对应2个线程)
2. 晾衣工2人
3. 共50件衣服
4. 洗衣工每2秒洗1件衣服
5. 晾衣工每1秒晾1件衣服
解决方案 »
- netbeans7.0的初次使用出现提示Have no file for D:\jdk1.6\jre\lib\modules\jdk.boot.jar
- java机制修改.bat内容
- 用JGraph做流程设计
- 能否通过jdbc创建一个新的数据库???
- 怎样检查目录是否为空
- 【急急急】怎么在panel上添加图形对象
- 关于iplanet白菜级问题.........
- 关于邮件群发的几个问题?
- 请问如果在另一台无装JMF和JDK的机上运行
- 静态方法
- swing 弹出对话框
- java.sql.SQLException: I/O Error: Connection resetCaused by: java.net.SocketExce
t3.start();
t2.start();
t4.start();这样就可以 生产一个拿走一个t1.start();
t2.start();
t3.start();
t4.start();已经加了同步方法 不是能实现资源共享嘛 为什么这样就不行了?t1,t2生产者 t3,t4消费者
public static void main(String[] args) {
int i = 10;
while(i > 0) {
new Producer().run();
new Consumer().run();
new Producer().run();
new Consumer().run();
i --;
}
}}
class Producer extends Thread { @Override
public void run() {
if (ProducerAndConsumer.A > 0) {
ProducerAndConsumer.A -= 1;
System.out.println("洗衣工:洗衣服");
if (ProducerAndConsumer.B + 1 <= 2)
ProducerAndConsumer.B += 1;
}
}}class Consumer extends Thread { @Override
public void run() {
if (ProducerAndConsumer.B > 0) {
ProducerAndConsumer.B -= 1;
System.out.println("晾衣工:晾衣服");
if (ProducerAndConsumer.A + 1 <= 2)
ProducerAndConsumer.A += 1;
}
}}
import java.util.List;public class Box{//装衣服的框
int count = 0;
List<String> clothes = new ArrayList<String>();
int index = 0;
int total = 20;
public Box(){
}
public synchronized void push(String name) throws InterruptedException{//入list同步
if(count>0){//有洗好的衣服,唤醒晾衣工
this.notifyAll();
}
else{
count++;
index++;
total--;
clothes.add(index+"");
System.out.println(name+":洗第"+index+"件衣服");
}
}
public synchronized void pop(String name){//出list同步
if(count>0)
{
count--;
String index = clothes.remove(0);
System.out.println(name + "晾第"+index+"件衣服");
}
else{
if(total!=0){//如果total=0,说明衣服洗完了,不要唤醒洗衣工.
try {
this.wait();
} catch (InterruptedException e){
e.printStackTrace();
}
this.notifyAll();
}
}
}
public static void main(String args[]){
Box box = new Box();
Consumer c1= new Consumer(box,"晾衣工1");
Consumer c2= new Consumer(box,"晾衣工2");
Producer p1 = new Producer(box,"洗衣工1");
Producer p2 = new Producer(box,"洗衣工2");
Thread thread11= new Thread(c1);
Thread thread12= new Thread(c2);
Thread thread21= new Thread(p1);
Thread thread22= new Thread(p2);
thread21.start();
thread22.start();
thread11.start();
thread12.start();
}
}class Consumer implements Runnable {//晾衣工
Box box;
String name;
public Consumer(Box box,String name){
this.box = box;
this.name = name;
}
@Override
public void run() {
while(box.total > 0||box.count>0){
try {
Thread.sleep(1000);
box.pop(name);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}class Producer implements Runnable {//洗衣工
private Box box;
String name; public Producer(Box box,String name) {
this.box = box;
this.name = name; }
public void run(){
while(box.total > 0){
try {
Thread.sleep(2000);
box.push(name);//把洗衣工的名字传过去。
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
package xy;public class Clothes {
private int soiledClothes = 50;
private int washOut = 0;
private int dry = 0; public synchronized boolean washOut() { if (soiledClothes != 0) {
washOut++;
--soiledClothes;
System.out.println(Thread.currentThread().getName() + ":洗第"
+ (50 - soiledClothes) + "件衣服");
notifyAll();
return true;
}
return false; } public synchronized int hangToDry() throws InterruptedException {
if (washOut == 0)
wait();
System.out.println(Thread.currentThread().getName() + ":晒第" + (dry + 1)
+ "件衣服");
washOut--;
return ++dry;
} public static void main(String[] args) {
// TODO Auto-generated method stub
Clothes clothes = new Clothes();
new Thread(new Laundress(clothes), "laundress1").start();
new Thread(new Laundress(clothes), "laundress2").start(); Thread hangToDry = new Thread(new HangToDry(clothes), "hangToDry");
hangToDry.setDaemon(true);
hangToDry.start();
}}class Laundress implements Runnable {
private Clothes clothes; public Laundress(Clothes clothes) {
super();
this.clothes = clothes;
} @Override
public void run() {
while (true) {
if (!clothes.washOut()) {
System.out.println(Thread.currentThread().getName() + ":无衣可洗");
break;
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("谁敢吵醒我");
}
} }}class HangToDry implements Runnable {
private Clothes clothes; public HangToDry(Clothes clothes) {
super();
this.clothes = clothes;
} @Override
public void run() {
while (true) {
try {
clothes.hangToDry();
} catch (InterruptedException e) { } try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
}
}
}