- class Ticket implements Runnable{
- private int tick = 100;
- Object o = new Object();
- public void run(){
- while(true){
- synchronized(o){
- if(tick > 0){
- try{Thread.sleep(10);}catch(Exception e){}
- System.out.println(currentThread().getName());
- }
- }
- }
- }
- }
- public class Demo{
- public static void main(String args[]){
- Ticket t = new Ticket();
- Thread t1 = new Thread(t);
- Thread t2 = new Thread(t);
- Thread t3 = new Thread(t);
- Thread t4 = new Thread(t);
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
复制代码
上面代码提示找不到currentThread()方法,这是为什么? |
|