Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. package aufgabe2;
  2.  
  3. public class LinksRechtsKau
  4. extends Thread
  5. {
  6. KauBoy kauboy;
  7. public LinksRechtsKau (KauBoy kauboy)
  8. {
  9. this.kauboy = kauboy;
  10. }
  11.  
  12. public void run()
  13. {
  14. System.out.println("Los gehts");
  15. synchronized(this)
  16. {
  17. while(!isInterrupted())
  18. {
  19. try{
  20. System.out.println(kauboy.name);
  21. Thread.sleep(2000);
  22. }
  23. catch(InterruptedException e)
  24. {
  25. System.out.println("Hee. warum unterbrichst du mich??");
  26. interrupt();
  27. }
  28. }
  29. }
  30. }
  31. }
  32.  
  33. package aufgabe2;
  34.  
  35. public class KauBoy
  36. {
  37. String name;
  38.  
  39. public KauBoy(String name)
  40. {
  41. this.name = name;
  42. }
  43. }
  44.  
  45.  
  46. package aufgabe2;
  47.  
  48. public class MyMain
  49. {
  50. public static void main(String[] args)
  51. throws InterruptedException
  52. {
  53. KauBoy k1 = new KauBoy("Links");
  54. KauBoy k2 = new KauBoy("Rechts");
  55. KauBoy k3 = new KauBoy("Kau");
  56.  
  57. LinksRechtsKau lrk1 = new LinksRechtsKau(k1);
  58. LinksRechtsKau lrk2 = new LinksRechtsKau(k2);
  59. LinksRechtsKau lrk3 = new LinksRechtsKau(k3);
  60.  
  61. lrk1.start();
  62. lrk2.start();
  63. lrk3.start();
  64.  
  65. Thread.sleep(10000);
  66.  
  67. lrk1.interrupt();
  68. lrk2.interrupt();
  69. lrk3.interrupt();
  70.  
  71. lrk1.join();
  72. lrk2.join();
  73. lrk3.join();
  74.  
  75. System.out.println("Und Schluß!");
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement