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;
}
}
本文提供了一个Java多线程程序实例,展示了如何通过两个不同的线程分别递增和递减整数变量,并周期性地输出这两个变量的值。该程序使用了自定义的Runnable实现类来创建线程任务。
940

被折叠的 条评论
为什么被折叠?



