Sharedresource类
class Sharedresource {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
// System.out.println("sharedresorce,while(false)");
try {
// /System.out.println("sharedresorce,while(false)object block");
wait();
// System.out.println("sharedresorce,while(false)object relise");
} catch (Exception e) {
// System.out.println("Thread Interrupted");
}
}
available = false;
notify();
return contents;
}
public synchronized void set(int value) {
while (available == true) {
try {
wait();
} catch (Exception e) {
System.out.println("Thread Interrupted");
}
}
contents = value;
available = true;
notify();
}
}
制作人类
class Produser extends Thread {
private Sharedresource resource;
private int number;
Produser(Sharedresource s, int number) {
resource = s;
this.number = number;
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
resource.set(i);
System.out.println("produser#" + this.number + "set:" + i);
// try{
// sleep(2000);
// }
// catch (Exception e) {
// System.out.println("tread intrupted");
// }
}
}
}
消费者阶层
class Consumer extends Thread {
private Sharedresource resource;
private int number;
public Consumer(Sharedresource s, int number) {
resource = s;
this.number = number;
}
int value = 0;
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
value = resource.get();
System.out.println("Consumer#" + this.number + "getvalue:" + value);
}
}
}
MAIN CLASS
public class SynchronizatonDemo121 {
public static void main(String[] args) {
Sharedresource r = new Sharedresource();
Produser p = new Produser(r, 1);
Consumer c = new Consumer(r, 1);
p.start();
c.start();
}
}
输出-1
Consumer#1getvalue:1
produser#1set:1
produser#1set:2
Consumer#1getvalue:2
produser#1set:3
Consumer#1getvalue:3
produser#1set:4
Consumer#1getvalue:4
produser#1set:5
Consumer#1getvalue:5
produser#1set:6
Consumer#1getvalue:6
Consumer#1getvalue:7
produser#1set:7
produser#1set:8
produser#1set:9
Consumer#1getvalue:8
Consumer#1getvalue:9
produser#1set:10
Consumer#1getvalue:10
输出-2
produser#1set:1
produser#1set:2
Consumer#1getvalue:1
Consumer#1getvalue:2
produser#1set:3
Consumer#1getvalue:3
produser#1set:4
Consumer#1getvalue:4
produser#1set:5
Consumer#1getvalue:5
produser#1set:6
Consumer#1getvalue:6
produser#1set:7
Consumer#1getvalue:7
produser#1set:8
Consumer#1getvalue:8
produser#1set:9
Consumer#1getvalue:9
produser#1set:10
Consumer#1getvalue:10
输出-3
produser#1set:1
Consumer#1getvalue:1
Consumer#1getvalue:2
produser#1set:2
produser#1set:3
Consumer#1getvalue:3
produser#1set:4
Consumer#1getvalue:4
produser#1set:5
Consumer#1getvalue:5
produser#1set:6
Consumer#1getvalue:6
Consumer#1getvalue:7
produser#1set:7
produser#1set:8
Consumer#1getvalue:8
Consumer#1getvalue:9
produser#1set:9
produser#1set:10
Consumer#1getvalue:10
根据生产者和消费者的行为,我的线程无法正常工作。 我已将我的输出附加在那里以便更好地理解,所以我的代码中的问题在哪里请验证我,提前谢谢
答案 0 :(得分:1)
您的制作人和消费者正按照预期的顺序制作和使用这些数字。但是,输出不同步,因为您不以同步方法打印它,因此每次执行时都会得到不同的顺序。
如果您将println
语句移至同步get
和set
方法:
public synchronized int get() {
while (available == false) {
// System.out.println("sharedresorce,while(false)");
try {
// /System.out.println("sharedresorce,while(false)object block");
wait();
// System.out.println("sharedresorce,while(false)object relise");
} catch (Exception e) {
// System.out.println("Thread Interrupted");
}
}
available = false;
notify();
System.out.println("Consumer#" + "getvalue:" + contents);
return contents;
}
public synchronized void set(int value) {
while (available == true) {
try {
wait();
} catch (Exception e) {
System.out.println("Thread Interrupted");
}
}
contents = value;
available = true;
System.out.println("produser#" + "set:" + value);
notify();
}
您将获得预期的输出:
produser#set:1
Consumer#getvalue:1
produser#set:2
Consumer#getvalue:2
produser#set:3
Consumer#getvalue:3
produser#set:4
Consumer#getvalue:4
produser#set:5
Consumer#getvalue:5
produser#set:6
Consumer#getvalue:6
produser#set:7
Consumer#getvalue:7
produser#set:8
Consumer#getvalue:8
produser#set:9
Consumer#getvalue:9
produser#set:10
Consumer#getvalue:10