62,614
社区成员
发帖
与我相关
我的任务
分享
public class ThreadTest6 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person_test6 pt = new Person_test6();
Input ip = new Input(pt);
Output op = new Output(pt);
Thread it = new Thread(ip);
Thread ot = new Thread(op);
it.start();
ot.start();
}
}
class Person_test6 {
private String Name;
private int Age;
private int x;
private boolean flag = false;
public synchronized void InputPerson() {
// 输入方法
while (true) {
if (flag == true) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
if (x == 1) {
this.Name = "Mark";
this.Age = 25;
System.out.println("mark is ready");
} else {
this.Name = "Rose";
this.Age = 22;
System.out.println("rose is ready");
}
}
x = (x + 1) % 2;
System.out.println("x=" + x);
flag = true;
System.out.println(flag);
this.notify();
}
}
public synchronized void OutputPerson() {
// 输出方法
while (true) {
if (flag == false) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
System.out.println(Name + "******" + Age);
flag = false;
System.out.println(flag);
this.notify();
}
}
}
class Input implements Runnable {
// 输入线程
private Person_test6 pt;
Input(Person_test6 pt) {
this.pt = pt;
}
public void run() {
while (true)
pt.InputPerson();
}
}
class Output implements Runnable {
// 输出线程
private Person_test6 pt;
Output(Person_test6 pt) {
this.pt = pt;
}
public void run() {
while (true)
pt.OutputPerson();
}
}
public synchronized void OutputPerson() {
// 输出方法
while (true) {
if (flag == false) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
System.out.println(Name + "******" + Age);
flag = false;
System.out.println(flag);
this.notify();
}
}
这里的
flag = false;
System.out.println(flag);
this.notify();
没有写在else里,放else里就行了
else{
System.out.println(Name + "******" + Age);
flag = false;
System.out.println(flag);
this.notify();
}
简写的ifelse害死人啊。。。。。package org.imzhs.learn.test;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args){
// TODO Auto-generated method stub
Person_test6 pt = new Person_test6();
Input ip = new Input(pt);
Output op = new Output(pt);
Thread it = new Thread(ip);
Thread ot = new Thread(op);
it.start();
ot.start();
}
}
class Person_test6 {
private String Name;
private int Age;
private int x;
private boolean flag = false;
public synchronized void InputPerson() {
// 输入方法
while (true) {
if (flag == true) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
if (x == 1) {
this.Name = "Mark";
this.Age = 25;
System.out.println("mark is ready");
} else {
this.Name = "Rose";
this.Age = 22;
System.out.println("rose is ready");
}
//以下提到else里
x = (x + 1) % 2;
System.out.println("x=" + x);
flag = true;
System.out.println(flag);
this.notify();
}
}
}
public synchronized void OutputPerson() {
// 输出方法
while (true) {
if (flag == false) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
{
//以下提到else里
System.out.println(Name + "******" + Age);
flag = false;
System.out.println(flag);
this.notify();
}
}
}
}
class Input implements Runnable {
// 输入线程
private Person_test6 pt;
Input(Person_test6 pt) {
this.pt = pt;
}
public void run() {
while (true)
pt.InputPerson();
}
}
class Output implements Runnable {
// 输出线程
private Person_test6 pt;
Output(Person_test6 pt) {
this.pt = pt;
}
public void run() {
while (true)
pt.OutputPerson();
}
}