代码如下:
public class TestThread{
public class DoThing{
private boolean valueSet = false;
private String name;
private int i;
public synchronized void Set(String name){
if(valueSet)
try{
wait();
}catch(InterruptedException e){
System.out.println("InterruptedException e");
}
valueSet = true;
notify();
System.out.println("pub: " + name);
}
public synchronized String Get(){
if(!valueSet)
try
{
wait();
}
catch(InterruptedException e){
System.out.println("InterruptedException e");
}
valueSet = false;
notify();
return name + ++i;
}
}
public class Tux implements Runnable{
DoThing t = new DoThing();
Tux(){
new Thread(this).start();
}
public void run(){
for(int i = 0;i < 5;i++)
{
t.Set("Tux");
}
}
}
public class Tux1 implements Runnable{
DoThing t = new DoThing();
Tux1(){
new Thread(this).start();
}
public void run(){
for(int i = 0;i < 5;i++)
{
System.out.println(t.Get());
}
}
}
public void GetTuxInstance(){
Tux tux = new Tux();
Tux1 tux1 = new Tux1();
}
public static void main(String[] args){
TestThread t1 = new TestThread();
t1.GetTuxInstance();
}
}
其运行后打印出来的值总是在等待,我哪块写错了,谢谢。
public class TestThread{
public class DoThing{
private boolean valueSet = false;
private String name;
private int i;
public synchronized void Set(String name){
if(valueSet)
try{
wait();
}catch(InterruptedException e){
System.out.println("InterruptedException e");
}
valueSet = true;
notify();
System.out.println("pub: " + name);
}
public synchronized String Get(){
if(!valueSet)
try
{
wait();
}
catch(InterruptedException e){
System.out.println("InterruptedException e");
}
valueSet = false;
notify();
return name + ++i;
}
}
public class Tux implements Runnable{
DoThing t = new DoThing();
Tux(){
new Thread(this).start();
}
public void run(){
for(int i = 0;i < 5;i++)
{
t.Set("Tux");
}
}
}
public class Tux1 implements Runnable{
DoThing t = new DoThing();
Tux1(){
new Thread(this).start();
}
public void run(){
for(int i = 0;i < 5;i++)
{
System.out.println(t.Get());
}
}
}
public void GetTuxInstance(){
Tux tux = new Tux();
Tux1 tux1 = new Tux1();
}
public static void main(String[] args){
TestThread t1 = new TestThread();
t1.GetTuxInstance();
}
}
其运行后打印出来的值总是在等待,我哪块写错了,谢谢。
解决方案 »
- SWT中浏览器对象调用HTML文件 相对路径怎么给?
- 谁有 The Definitive Guide to SWT and JFace的pdf啊
- 如何将一个只读文件放进zip包,并用winZip解压后仍然是只读属性
- 我是一个编程初学者,想请教高手们一个关于java的问题
- 如何给输入出生日期的文本框,加入一个可以选择时间的控件,像window clock 那样的?
- 请输入如下代码的输出结果,并解释原因
- java 传值问题
- 谁帮我看下这个程序 编译通过 但是运行出错!
- paint一个image如何设置角度?
- 窗体拖动问题!!
- 字节数组[]转换成图象文件,文件保存为空
- 需求工程的一道题目,熟悉点人的帮我解答下
下面我为你做了一下修改.
package testTwo;public class TestThread { public class DoThing {
private boolean valueSet = false; private String name; private int i; public synchronized void Set(String name) {
this.name=name;
if (valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException e");
}
valueSet = true;
notify();
System.out.println("pub: " + name);
} public synchronized String Get() {
if (!valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException e");
}
valueSet = false;
notify();
return name + ++i;
}
} public class Tux implements Runnable {
DoThing t ; Tux(DoThing t) {
this.t=t;
new Thread(this).start();
} public void run() {
for (int i = 0; i < 5; i++) {
t.Set("Tux");
}
}
} public class Tux1 implements Runnable {
DoThing t; Tux1(DoThing t) {
this.t=t;
new Thread(this).start();
} public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(t.Get());
}
}
} public void GetTuxInstance() {
DoThing t = new DoThing();
Tux tux = new Tux(t);
Tux1 tux1 = new Tux1(t);
} public static void main(String[] args) {
TestThread t1 = new TestThread();
t1.GetTuxInstance();
}
}
你两个线程都初始化了DoThing,所以两个线程间运行的不是一个DoThing
明白了。多个线程需要围绕同一个对象进行同步的,我这个理解是吧。