Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aufgabe2;
- public class LinksRechtsKau
- extends Thread
- {
- KauBoy kauboy;
- public LinksRechtsKau (KauBoy kauboy)
- {
- this.kauboy = kauboy;
- }
- public void run()
- {
- System.out.println("Los gehts");
- synchronized(this)
- {
- while(!isInterrupted())
- {
- try{
- System.out.println(kauboy.name);
- Thread.sleep(2000);
- }
- catch(InterruptedException e)
- {
- System.out.println("Hee. warum unterbrichst du mich??");
- interrupt();
- }
- }
- }
- }
- }
- package aufgabe2;
- public class KauBoy
- {
- String name;
- public KauBoy(String name)
- {
- this.name = name;
- }
- }
- package aufgabe2;
- public class MyMain
- {
- public static void main(String[] args)
- throws InterruptedException
- {
- KauBoy k1 = new KauBoy("Links");
- KauBoy k2 = new KauBoy("Rechts");
- KauBoy k3 = new KauBoy("Kau");
- LinksRechtsKau lrk1 = new LinksRechtsKau(k1);
- LinksRechtsKau lrk2 = new LinksRechtsKau(k2);
- LinksRechtsKau lrk3 = new LinksRechtsKau(k3);
- lrk1.start();
- lrk2.start();
- lrk3.start();
- Thread.sleep(10000);
- lrk1.interrupt();
- lrk2.interrupt();
- lrk3.interrupt();
- lrk1.join();
- lrk2.join();
- lrk3.join();
- System.out.println("Und Schluß!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement