黑马程序员技术交流社区
标题:
关于多线程wait和notify的问题
[打印本页]
作者:
孙沛
时间:
2012-9-10 10:53
标题:
关于多线程wait和notify的问题
上午学了锁,敲了代码,可是怎么调试都报错java.lang.IllegalMonitorStateExceptionimport java.lang.Thread;
class Res{
String name;
String sex;
boolean flag = false;
}
class Input implements Runnable{
private Res r;
Input(Res r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
synchronized (r){
if(r.flag){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(x == 0){
r.name = "nike";
r.sex = "man";
}else{
r.name = "apple";
r.sex = "girl";
}
x = (x+1)%2;
r.flag = true;
r.notify();
}
}
}
}
class Output implements Runnable{
private Res r;
Output(Res r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
synchronized (r){
if(!r.flag){
try {
r.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(r.name+"..."+r.sex);
r.flag = false;
r.notify();
}
}
}
}
class InputOutputDemo{
public static void main(String[] args){
Res r = new Res();
Input in = new Input(r);
Output out = new Output(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
也不知道错在哪里,检查不出来,麻烦解决下
作者:
孙沛
时间:
2012-9-10 11:02
哈哈,我知道哪里错了
wait的时候,没有指定对象
作者:
杨震
时间:
2012-9-10 11:03
class Res{
String name;
String sex;
boolean flag = false;
}
class Input implements Runnable{
private Res r;
Input(Res r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
synchronized (r){
if(r.flag){
try {
r.wait();//这里改成r.wait()
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(x == 0){
r.name = "nike";
r.sex = "man";
}else{
r.name = "apple";
r.sex = "girl";
}
x = (x+1)%2;
r.flag = true;
r.notify();
}
}
}
}
class Output implements Runnable{
private Res r;
Output(Res r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
synchronized (r){
if(!r.flag){
try {
r.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(r.name+"..."+r.sex);
r.flag = false;
r.notify();
}
}
}
}
class InputOutputDemo{
public static void main(String[] args){
Res r = new Res();
Input in = new Input(r);
Output out = new Output(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
作者:
高铭
时间:
2012-9-10 11:05
Input 类里面的wait() 前面要加上 r. 不然怎么会知道谁wait了
作者:
张忠豹
时间:
2012-9-11 23:08
package com.itheima.test;
class Res{
String name;
String sex;
boolean flag = false;
}
class Input implements Runnable{
private Res r;
Input(Res r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
synchronized (r){
if(!r.flag){//①
try {
r.wait();//②
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(x == 0){
r.name = "nike";
r.sex = "man";
}else{
r.name = "apple";
r.sex = "girl";
}
x = (x+1)%2;
r.flag = true;
r.notify();
}
}
}
}
class Output implements Runnable{
private Res r;
Output(Res r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
synchronized (r){
if(r.flag){//③
try {
r.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(r.name+"..."+r.sex);
r.flag = false;
r.notify();
}
}
}
}
public class InputOutputDemo{
public static void main(String[] args){
Res r = new Res();
Input in = new Input(r);
Output out = new Output(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
在①、②和③处做程序中的修改,亦可以实现
作者:
陈俊来
时间:
2012-9-12 11:28
class Res{
String name;
String sex;
boolean flag = false;
}
class Input implements Runnable{
private Res r;
Input(Res r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
synchronized (r){
if(r.flag){
try {
r.wait();//
这里要为其指定对象的
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(x == 0){
r.name = "nike";
r.sex = "man";
}else{
r.name = "apple";
r.sex = "girl";
}
x = (x+1)%2;
r.flag = true;
r.notify();
}
}
}
}
class Output implements Runnable{
private Res r;
Output(Res r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
synchronized (r){
if(!r.flag){
try {
r.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(r.name+"..."+r.sex);
r.flag = false;
r.notify();
}
}
}
}
class InputOutputDemo{
public static void main(String[] args){
Res r = new Res();
Input in = new Input(r);
Output out = new Output(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2