Advertisement
JamesHsieh

Synch

Jul 20th, 2013
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. class Callme{
  2. void call(String msg){
  3. System.out.println("["+msg);
  4. try {
  5. Thread.sleep(1000);
  6.  
  7. } catch (InterruptedException e) {
  8. // TODO: handle exception
  9. System.out.println("Interrupted.");
  10. }
  11. System.out.println("]");
  12. }
  13. }
  14. public class Caller implements Runnable {
  15.  
  16. String msg;
  17. Callme target;
  18. Thread t;
  19. public Caller(Callme targ,String s) {
  20. target=targ;
  21. msg=s;
  22. t=new Thread(this);
  23. t.start();
  24. }
  25. @Override
  26. public void run() {
  27. target.call(msg);
  28. // TODO Auto-generated method stub
  29.  
  30. }
  31.  
  32. }
  33. class Synch
  34. {
  35. public static void main(String args[]){
  36. Callme target=new Callme();
  37. Caller ob[]={
  38. new Caller(target, "Hellow"),
  39. new Caller(target, "Synchronized"),
  40. new Caller(target, "World")
  41. };
  42. try {
  43. ob[0].t.join();
  44. ob[1].t.join();
  45. ob[2].t.join();
  46. } catch (InterruptedException e) {
  47. // TODO: handle exception
  48. System.out.println("Interrupted.");
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement