为什么不能用在Resource里定义的flag?

忧桑的民工 2017-12-21 10:18:08

问题:为什么不能用在Resource里定义的flag?

源码如下:


public class ThreadOfCommunication {
public static void main(String[] args) {
// 创建资源
Resource resource2 = new Resource();
// 创建任务
Input input = new Input(resource2);
Output output = new Output(resource2);
// 创建线程,执行路径
Thread thread1 = new Thread(input);
Thread thread2 = new Thread(output);
// 开启线程
thread1.start();
thread2.start();

}

}

/*
* Resource(资源)
*/
class Resource {
String name;
String sex;
boolean flag=false;
}

/*
* input(输入)
*/
class Input implements Runnable {
Resource resource;

public Input(Resource resource) {
this.resource = resource;
}

public void run() {
int x = 0;
while (true) {
synchronized (resource) {
if(flag)
wait();
if (x == 0) {
resource.name = "PIG";
resource.sex = "MAN";
} else {
resource.name = "丽丽";
resource.sex = "女";
}
flag=true;
notify();
}
x = (x + 1) % 2;
}

}
}

/*
* output(输出)
*/
class Output implements Runnable {
Resource resource;

public Output(Resource resource) {
this.resource = resource;
}

public void run() {
while (true) {// 无限循环所以用while
synchronized (resource) {
if(!flag)
wait();
System.out.println(
Thread.currentThread().getName() + ";刑满释放人员姓名::" + resource.name + ";刑满释放人员性别:" + resource.sex);
}
}
}
}

求解?


...全文
154 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
自由自在_Yu 2017-12-22
  • 打赏
  • 举报
回复
Input和Resource都不是同一个类,怎么可能直接用Resource里面的变量呢
public class ThreadOfCommunication {
	public static void main(String[] args) {
		// 创建资源
		Resource resource2 = new Resource();
		// 创建任务
		Input input = new Input(resource2);
		Output output = new Output(resource2);
		// 创建线程,执行路径
		Thread thread1 = new Thread(input);
		Thread thread2 = new Thread(output);
		// 开启线程
		thread1.start();
		thread2.start();

	}

}

/*
 * Resource(资源)
 */
class Resource {
	String name;
	String sex;
    boolean flag = false;
}

/*
 * input(输入)
 */
class Input implements Runnable {
	Resource resource;

	public Input(Resource resource) {
		this.resource = resource;
	}

	public void run() {
		int x = 0;
		while (true) {
			synchronized (resource) {
				if (resource.flag)
					try {
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				if (x == 0) {
					resource.name = "PIG";
					resource.sex = "MAN";
				} else {
					resource.name = "丽丽";
					resource.sex = "女";
				}
				resource.flag = true;
				notify();
			}
			x = (x + 1) % 2;
		}

	}
}

/*
 * output(输出)
 */
class Output implements Runnable {
	Resource resource;

	public Output(Resource resource) {
		this.resource = resource;
	}

	public void run() {
		while (true) {// 无限循环所以用while
			synchronized (resource) {
				if (!resource.flag)
					try {
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				System.out.println(Thread.currentThread().getName()
						+ ";刑满释放人员姓名::" + resource.name + ";刑满释放人员性别:"
						+ resource.sex);
			}
		}
	}
忧桑的民工 2017-12-22
  • 打赏
  • 举报
回复
明白了!谢谢!
忧桑的民工 2017-12-22
  • 打赏
  • 举报
回复
Resource resource;不是创建了资源任务吗? 这不可以调用吗?

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧