2016-09-22 40 views
0

如果我知道特定的线程ID。如何做到以下几点?在给定线程ID的情况下在特定线程上运行方法

主题。 getThreadById(id).continueWork();

可能吗?

public class Test implements Runnable { 

public void run() { 
    while(true){ 
    pause(); 

    doSomework(); 
    } 
} 

private void doSomework() { 
    System.out.println("do some work"); 
} 

public synchronized void pause() { 
    if (Tester.waitCondition == true) { 
     try { 
      wait(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

public synchronized void continueWork() { 
    notify(); 
} 
} 

public class Tester { 

public static boolean waitCondition = true; 

public static void main(String[] args) { 
    Thread nThread = new Thread(new Test()); 
    nThread.start(); 
    waitCondition = false; 
    Thread nThread1 = new Thread(new Test()); 
    nThread1.start(); 
    Thread nThread2 = new Thread(new Test()); 
    nThread2.start(); 
    Thread nThread3 = new Thread(new Test()); 
    nThread3.start(); 

    Long id = nThread.getId(); 
    Thread.getThreadById(id).continueWork(); 


} 
} 
+0

不,不是这样。你需要安排不同的'waitCondition's,每个线程都有自己的一个。 –

+0

请您详细说明一下吗? –

+0

我知道这是不可能的。但我想要一个办法做到这一点 –

回答

0

诠:

public class Tester { 
    // Apologies, I'm too lazy to create two separate files 
    static public class Test implements Runnable { 

    private void doSomework() { 
     System.out.print(
      "do some work on Thread: " 
     +Thread.currentThread().getId() 
    ); 
     try { 
     Thread.sleep(500); // just to simulate a load 
     } 
     catch(InterruptedException e) { 
     // ignore 
     } 
    } 

    public void run() { 
     do { 
     boolean shouldIWait=true; 
     synchronized(Tester.lockingObj) { 
      Boolean flag=Tester.waitConditions.get(Thread.currentThread().getId()); 
      if(null!=flag) { 
      shouldIWait=flag.booleanValue(); 
      } // if null, the tester started me before creating my flag. I'll wait 
      if(shouldIWait) { 
      // I need to wait for someone to wake me 
      try { 
       Tester.lockingObj.wait(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
       // well, I'm interrupted, so I'll do no more work. 
       break; 
      } 
      } 
     } 
     if(false==shouldIWait) { 
      // waiting no more 
      this.doSomework(); 
     } 
     } while(true); 
    } 

    } 

    public static Object lockingObj=new Object(); 

    public static TreeMap<Long, Boolean> waitConditions= 
     new TreeMap<Long, Boolean>(); 

    public static void main(String[] args) { 
    Thread nThread = new Thread(new Test()); 
    Thread nThread1 = new Thread(new Test()); 
    Thread nThread2 = new Thread(new Test()); 
    Thread nThread3 = new Thread(new Test()); 

    // when starting, all threads will be waiting 
    waitConditions.put(nThread.getId(), true); 
    waitConditions.put(nThread.getId(), true); 
    waitConditions.put(nThread.getId(), true); 
    waitConditions.put(nThread.getId(), true); 

    nThread2.start(); 
    nThread1.start(); 
    nThread.start(); 
    nThread3.start(); 

    Long id = nThread.getId(); 
    synchronized (lockingObj) { // when notified, all thread should wakeup 
     waitConditions.put(id, false); // but only nThread will be allowed to doSomeWork 
     lockingObj.notifyAll(); // wake up all the threads. 
           // Those not allowed, will go into 
           // back waiting 
    } 

    try { 
     // just to have the main thread still running for a while 
     Thread.sleep(3000); 
    } catch (InterruptedException e) { 
    } 
    // maybe we want to switch of nThread and start another? 
    synchronized (lockingObj) { 
     waitConditions.put(id, true); 
     waitConditions.put(nThread1.getId(), false); 
     lockingObj.notifyAll(); 
    } 
    try { 
     // just to have the main thread still running for a while 
     Thread.sleep(3000); 
    } catch (InterruptedException e) { 
    } 

    } 
} 
+0

这是个好主意。但是当你提到“//通知之后,nThread应该启动”它实际上并不是正确的? 它只有在锁定对象通知所有 –

+0

@AsiriLiyanaArachchi之后才会启动 - 它将暂时启动,在waitingConditions中查看其条目,如果不允许执行某些操作,它将回到等待状态。 –

+0

我从你的逻辑中了解到每个线程在开始时处于等待位置。当您将waitingCondition设置为false时,它将准备好无限制地循环,并且一旦在lockingObject上通知我们做出waitCondition false的线程将继续运行。其他线程也会运行一次,并再次陷入等待状态。 –

1

您需要使用锁来阻塞线程,然后调用锁的notify方法来设置阻塞的线程可运行。
如果不止一个线程要继续,您将需要Condition
像打击:

final Lock lock = new ReentrantLock(); 

final Condition condition1 = lock.newCondition(); 
final Condition condition2 = lock.newCondition(); 

Thread t = new Thread() { 
    @Override 
    public void run() { 

    try { 
     lock.lock(); 
     condition1.await(); 
     System.out.println("end cdt1"); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     lock.unlock(); 
    } 
    } 
}; 

t.start(); 

Thread t1 = new Thread() { 
    @Override 
    public void run() { 

    try { 
     lock.lock(); 
     condition2.await(); 
     System.out.println("end cdt2"); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     lock.unlock(); 
    } 

    } 
}; 
t1.start(); 

Thread.sleep(1000); 

Thread tt = new Thread() { 
    @Override 
    public void run() { 
    try { 
     lock.lock(); 
     condition1.signal(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     lock.unlock(); 
    } 
    } 
}; 
tt.start(); 

Thread.sleep(2000); 

Thread tt1 = new Thread() { 
    @Override 
    public void run() { 
    try { 
     lock.lock(); 
     condition2.signal(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     lock.unlock(); 
    } 
    } 
}; 
tt1.start(); 
+0

,你可以看到有多个线程。如果我将锁对象传递给一个线程,并且两个线程正在等待它将知道哪个线程应该继续以及哪个线程应该保持暂停状态 –

+0

@AsiriLiyanaArachchi如果是这样,则需要使用Condition。 – passion

相关问题