Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1.  
  2. import java.util.concurrent.Semaphore;
  3. class latchestest1{
  4. public static void main(String[]args){
  5. Semaphore sem = new Semaphore(0);
  6.  
  7. for(int j = 0; j<10; j++){
  8. new TA(sem, j).start();
  9. }
  10.  
  11. System.out.println("Threads under starters orders");
  12. try{
  13. Thread.sleep(3000);
  14. }catch(InterruptedException e){}
  15. System.out.println("Starting threads now");
  16. sem.release(10);
  17. }
  18. }
  19.  
  20. class TA extends Thread{
  21.  
  22. Semaphore sem;
  23. int num;
  24.  
  25. public TA (Semaphore s, int n){
  26. sem = s; num = n;
  27. }
  28.  
  29. public void run(){
  30. //wait for signal to go
  31.  
  32. try {
  33. sem.acquire();
  34. } catch (InterruptedException e) {
  35. // TODO Auto-generated catch block
  36.  
  37. }
  38.  
  39. System.out.println("Thread" +num+ "going");
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement