牙狼garo电视剧第一季:初学者的多线程问题

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/29 14:27:10
public class Mythread{
static int i=100;
Mythread pool= new Mythread();
Out aa=new Out();
In bb= new In();

public static void main(String [] args){
pool(aa).start();
pool(bb).start();
}
}
class Out implements Runnable
{ static public void run()
{
while(i>0){
i=i-2;
System.out.println("flows"+i);}
}

}
class In implements Runnable
{
static public void run()
{ while(i<100){
i=i+1;
System.out.println("inject"+i);}
}
}
请问 1.我创建对象i 可是在In和Out类中都不能使用为什么```帮我改下啊```
2.pool(aa).start();
pool(bb).start();
这种方式能启动多线成吗

要达到你要的预期行为,改动MyThread如下:
public class Mythread{
static int i=100;
// Mythread pool= new Mythread();
static Out aa=new Out();//加上static
static In bb= new In();//加上static

public static void main(String [] args){
//pool(aa).start();
new MyThread(aa).start();
// pool(bb).start();
new MyThread(bb).start();
}
}