62,628
社区成员
发帖
与我相关
我的任务
分享
public class MyRunnable implements Runnable{
private int n = 10;
@Override
public void run() {
// TODO Auto-generated method stub
while (n > 0) {
System.out.println(Thread.currentThread().getName()+":"+n--);
}
}
}
public class DemoThread {
public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
Thread t1 = new Thread(mr, "线程1");
Thread t2 = new Thread(mr, "线程2");
t1.start();
t2.start();
}
}
public class MyRunnable implements Runnable{
private int n = 10;
@Override
public void run() {
while (true) {
int v = getN();
if (v <= 0) {
break;
}
System.out.println(Thread.currentThread().getName()+":"v);
}
}
private synchronized int getN() {
int v = n;
n--;
return v;
}
}