题目:编写一个程序:创建两个线程,其中一个产生5个随机整数,另一个线程将这五个数相加?
我编的代码如下:但就是出现一个问题:其中产生5个随机整数的线程不能唤醒另一个线程,想让那位高手帮我看看,能否解决???
import java.util.Random;
class valuebuf{
private Random generator=new Random();
public static int total;
private static boolean isadder=true;
public synchronized void createFiveNum(int num1,int num2,int num3,int num4,int num5){
num1=generator.nextInt(10);
num2=generator.nextInt(10);
num3=generator.nextInt(10);
num4=generator.nextInt(10);
num5=generator.nextInt(10);
System.out.println("num1="+num1);
System.out.println("num2="+num2);
System.out.println("num3="+num3);
System.out.println("num4="+num4);
System.out.println("num5="+num5);
total=num1+num2+num3+num4+num5;
System.out.println("the total in createFiveNum 1 is:"+total);
isadder=false;
notifyAll();
System.out.println("the total in createFiveNum 2 is:"+total);
notifyAll();
notifyAll();
}
public synchronized void addFiveNum(){
System.out.println(isadder);
if(isadder){
try{
System.out.println("vathread is begining to wait!!");
this.wait();
System.out.println("vathread is finished waiting!!");
}catch(InterruptedException e){
System.out.println("threadgenerator is interrupted"+e);
}
}
System.out.println("the total in vathread is:"+this.total);
}
}
class threadgenerator extends Thread{
valuebuf vgthread;
public int num1,num2,num3,num4,num5;
public threadgenerator(){
vgthread=new valuebuf();
}
public void run(){
vgthread.createFiveNum(num1,num2,num3,num4,num5);
}
}class threadadder extends Thread{
public int total;
valuebuf vathread;
public threadadder(){
vathread=new valuebuf();
}
public void run(){
vathread.addFiveNum();
}
}public class homework7_4_3_second {
threadadder adder;
threadgenerator testgenerator;
public homework7_4_3_second(){
testgenerator=new threadgenerator();
adder=new threadadder();
adder.start();
testgenerator.start(); //交换了启动顺序,让adder不进入wait(),看看这种情况;
}
public static void main (String[] args) {
new homework7_4_3_second();
System.out.println("activecount="+Thread.activeCount());
}
}
我编的代码如下:但就是出现一个问题:其中产生5个随机整数的线程不能唤醒另一个线程,想让那位高手帮我看看,能否解决???
import java.util.Random;
class valuebuf{
private Random generator=new Random();
public static int total;
private static boolean isadder=true;
public synchronized void createFiveNum(int num1,int num2,int num3,int num4,int num5){
num1=generator.nextInt(10);
num2=generator.nextInt(10);
num3=generator.nextInt(10);
num4=generator.nextInt(10);
num5=generator.nextInt(10);
System.out.println("num1="+num1);
System.out.println("num2="+num2);
System.out.println("num3="+num3);
System.out.println("num4="+num4);
System.out.println("num5="+num5);
total=num1+num2+num3+num4+num5;
System.out.println("the total in createFiveNum 1 is:"+total);
isadder=false;
notifyAll();
System.out.println("the total in createFiveNum 2 is:"+total);
notifyAll();
notifyAll();
}
public synchronized void addFiveNum(){
System.out.println(isadder);
if(isadder){
try{
System.out.println("vathread is begining to wait!!");
this.wait();
System.out.println("vathread is finished waiting!!");
}catch(InterruptedException e){
System.out.println("threadgenerator is interrupted"+e);
}
}
System.out.println("the total in vathread is:"+this.total);
}
}
class threadgenerator extends Thread{
valuebuf vgthread;
public int num1,num2,num3,num4,num5;
public threadgenerator(){
vgthread=new valuebuf();
}
public void run(){
vgthread.createFiveNum(num1,num2,num3,num4,num5);
}
}class threadadder extends Thread{
public int total;
valuebuf vathread;
public threadadder(){
vathread=new valuebuf();
}
public void run(){
vathread.addFiveNum();
}
}public class homework7_4_3_second {
threadadder adder;
threadgenerator testgenerator;
public homework7_4_3_second(){
testgenerator=new threadgenerator();
adder=new threadadder();
adder.start();
testgenerator.start(); //交换了启动顺序,让adder不进入wait(),看看这种情况;
}
public static void main (String[] args) {
new homework7_4_3_second();
System.out.println("activecount="+Thread.activeCount());
}
}
应该在创建线程的类homework7_4_3_second中创建valuebuf对象,而两个线程都保存这个对象的引用;这样这两个线程有了共同的资源,wait,notify操作也才有意义。
http://www.javaif.com/html/jiaocheng/20071215/569.html
public static void main(String[] args) {
Factroy factroy=new Factroy();
Produce produce=new Produce(factroy);
Customer customer =new Customer(factroy);
new Thread(produce).start();
new Thread(customer).start();
}
}class Factroy {
private int[] a = new int[5];
private boolean signal = false; synchronized void produce() {
if (!signal) {
for (int i = 0; i < 5; i++) {
a[i] = new Random().nextInt();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
signal = true;
notify();
} else {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} synchronized void customer() {
if (signal) {
int sum = 0;
for (int i = 0; i < 5; i++) {
System.out.println("Num" + i + ":=\t" + a[i]);
sum += a[i];
}
System.out.println("The sum of these numbers is" + sum);
signal=false;
notify();
}
else{
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class Produce implements Runnable {
private Factroy factroy; Produce(Factroy factroy) {
this.factroy = factroy;
} public void run() {
factroy.produce();
}
}class Customer implements Runnable {
private Factroy factroy; Customer(Factroy factroy) {
this.factroy = factroy;
} public void run() {
factroy.customer();
}
}