Understanding join() : Simple Threads « Threads « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Threads » Simple ThreadsScreenshots 
Understanding join()
Understanding join()


// : c13:Joining.java
// Understanding join().
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

class Sleeper extends Thread {
  private int duration;

  public Sleeper(String name, int sleepTime) {
    super(name);
    duration = sleepTime;
    start();
  }

  public void run() {
    try {
      sleep(duration);
    catch (InterruptedException e) {
      System.out.println(getName() " was interrupted. "
          "isInterrupted(): " + isInterrupted());
      return;
    }
    System.out.println(getName() " has awakened");
  }
}

class Joiner extends Thread {
  private Sleeper sleeper;

  public Joiner(String name, Sleeper sleeper) {
    super(name);
    this.sleeper = sleeper;
    start();
  }

  public void run() {
    try {
      sleeper.join();
    catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
    System.out.println(getName() " join completed");
  }
}

public class Joining {

  public static void main(String[] args) {
    Sleeper sleepy = new Sleeper("Sleepy"1500), grumpy = new Sleeper(
        "Grumpy"1500);
    Joiner dopey = new Joiner("Dopey", sleepy), doc = new Joiner("Doc",
        grumpy);
    grumpy.interrupt();

  }
///:~


           
       
Related examples in the same category
1. Thread ReminderThread Reminder
2. Thread Race DemoThread Race Demo
3. Suggesting when to switch threads with yield()Suggesting when to switch threads with yield()
4. Creating threads with inner classesCreating threads with inner classes
5. The safe way to stop a thread
6. Calling sleep() to wait for a whileCalling sleep() to wait for a while
7. Daemon threads spawn other daemon threadsDaemon threads spawn other daemon threads
8. Daemon threads don't prevent the program from ending.
9. Shows the use of thread priorities.Shows the use of thread priorities.
10. Java new feature: threadingJava new feature: threading
11. Java 1.5 (5.0) new feature: Thread ScheduleJava 1.5 (5.0) new feature: Thread Schedule
12. Two simple threadsTwo simple threads
13. Simple threads creator Simple threads creator
14. Task
15. SimpleThread using the Runnable interface.SimpleThread using the Runnable interface.
16. Very simple Threading example.Very simple Threading example.
17. Test Override ThreadTest Override Thread
18. Test Override
19. Parallelizing Loops for Multiprocessor Machines
20. Three Threads TestThree Threads Test
w__ww__.java__2___s.__c___o_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.