public class ThreadTT {

/**
 * Method main
 *
 *
 * @param args
 *
 */
static String str="";
public static void main(String[] args) {
// TODO: Add your code here
new Thread(new Say()).start();
int i=0;
while(true)
{
synchronized(str)
{
if(i==5000)
return;
System.out.println("ni hao.");
i++;
}
}
} class Say implements Runnable
{
public void run()
{
int i=0;
while(true)
{
synchronized(str)
{
if(i==5000)
return;
System.out.println("hello world.");
i++;
}
}
}
}
}
求哪为大哥指点迷津!

解决方案 »

  1.   

    有两种方法能解决你得问题
    ××××××××××××
    ×× 第一种 ×××
    ××××××××××××
    定义clas Say 为static,使Say 完全独立出来
    比如
    static class Say implements Runnable ....××××××××××××
    ×× 第二种 ×××
    ××××××××××××
    在main 中,用ThreadTT 这个类的实体创建Say实体
    比如
    public static void main(String[] args) {
    ....
    ThreadTT t = new ThreadTT();
    new Thread(t.new Say()).start();
    ....
    }
    注意这里 t.new 说明ThreadTT 和 Say有包含关系,所以需要一个ThreadTT实体