您的位置:首页 > 编程语言 > Java开发

简单的Java多线程演示

2008-02-20 20:07 357 查看
import java.util.*;

public class Tshow{
Runnable r1 = new t1();
Runnable r2 = new t2();
Thread th1 = new Thread(r1);
Thread th2 = new Thread(r2);
public void GetInts() throws Exception{
System.out.println("Int1=" + Integer.toString(((t1)r1).getX()) + "/t/tIntY=" + Integer.toString(((t2)r2).getY()));
Thread.sleep(100);
GetInts();
}
public void loop(){
try{
th1.start();
th2.start();
GetInts();
}catch(Exception e){System.out.println(e);}
}
public static void main(String agrt[]){
Tshow k = new Tshow();
k.loop();
}
}

class t1 implements Runnable{
int intX = 0;
public void run(){
try{
intX++;
Thread.sleep(300);
run();
}catch(Exception e){}
}
public int getX(){
return intX;
}
}
class t2 implements Runnable{
int intY = 1000000;
public void run(){
try{
intY--;
Thread.sleep(100);
run();
}catch(Exception e){}
}
public int getY(){
return intY;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: