Creating Thread Objects: Implementing the run() Method in Runnable interface : Create Thread « Thread « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Regular Expressions
8. Collections
9. Thread
10. File
11. Generics
12. I18N
13. Swing
14. Swing Event
15. 2D Graphics
16. SWT
17. SWT 2D Graphics
18. Network
19. Database
20. JSP
21. JSTL
22. Servlet
23. Web Services SOA
24. Email
25. J2EE Application
26. XML
27. Design Pattern
28. Log
29. Apache Common
Microsoft Office Word 2007 Tutorial
Java
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 Tutorial » Thread » Create Thread 
9. 1. 3. Creating Thread Objects: Implementing the run() Method in Runnable interface

import java.io.IOException;

class TryThread implements Runnable {
  public TryThread(String firstName, String secondName, long delay) {
    this.firstName = firstName;
    this.secondName = secondName;
    aWhile = delay;
  }
  public void run() {
    try {
      while (true) {
        System.out.print(firstName);
        Thread.sleep(aWhile);
        System.out.print(secondName + "\n");
      }
    catch (InterruptedException e) {
      System.out.println(firstName + secondName + e);
    }
  }
  private String firstName;
  private String secondName;
  private long aWhile;
}
public class MainClass {
  public static void main(String[] args) {
    Thread first = new Thread(new TryThread("A ""a "200L));
    Thread second = new Thread(new TryThread("B ""b "300L));
    Thread third = new Thread(new TryThread("C ""c "500L));
    System.out.println("Press Enter when you have had enough...\n");
    first.start();
    second.start();
    third.start();
    try {
      System.in.read();
      System.out.println("Enter pressed...\n");
    catch (IOException e) {
      System.out.println(e);
    }
    System.out.println("Ending main()");
    return;
  }
}
    
9. 1. Create Thread
9. 1. 1. Creating a Thread
9. 1. 2. Creating Thread: Deriving a Subclass of Thread
9. 1. 3. Creating Thread Objects: Implementing the run() Method in Runnable interface
w_ww_._j_a_v_a__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.