public class thread {
public static void main(String[] args) {
// TODO Auto-generated method stub
Num num = new Num();
Thread counter1 = new Thread(num);
Thread counter2 = new Thread(num);
counter1.start();
counter2.start();
for(int i = 0; i < 10000; i++)
{
num.testEquals();
try {
Thread.sleep(1);
}catch(Exception e) {
}
}
}
}
class Num extends Thread{
private int x = 0;
private int y = 0;
synchronized public void run() {
while(true)
{
x++;
y++;
}
}
boolean testEquals() {
boolean ok = (x==y);
System.out.println(x + "," + y + " : " + ok);
return ok;
}
}
java小白自学开线程很容易让电脑死机(内附代码)
最新推荐文章于 2024-04-20 15:24:27 发布