- /*
- Create New Thread Using Runnable Example
- This Java example shows how to create a new thread by implementing
- Java Runnable interface.
- */
- /*
- * To create a thread using Runnable, a class must implement
- * Java Runnable interface.
- */
- public class CreateThreadRunnableExample implements Runnable{
- /*
- * A class must implement run method to implement Runnable
- * interface. Signature of the run method is,
- *
- * public void run()
- *
- * Code written inside run method will constite a new thread.
- * New thread will end when run method returns.
- */
- public void run(){
- for(int i=0; i < 5; i++){
- System.out.println("Child Thread : " + i);
- try{
- Thread.sleep(50);
- }
- catch(InterruptedException ie){
- System.out.println("Child thread interrupted! " + ie);
- }
- }
- System.out.println("Child thread finished!");
- }
- public static void main(String[] args) {
- /*
- * To create new thread, use
- * Thread(Runnable thread, String threadName)
- * constructor.
- *
- */
- Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread");
- /*
- * To start a particular thread, use
- * void start() method of Thread class.
- *
- * Please note that, after creation of a thread it will not start
- * running until we call start method.
- */
- t.start();
- for(int i=0; i < 5; i++){
- System.out.println("Main thread : " + i);
- try{
- Thread.sleep(100);
- }
- catch(InterruptedException ie){
- System.out.println("Child thread interrupted! " + ie);
- }
- }
- System.out.println("Main thread finished!");
- }
- }
- /*
- Typical output of this thread example would be
- Main thread : 0
- Child Thread : 0
- Child Thread : 1
- Main thread : 1
- Main thread : 2
- Child Thread : 2
- Child Thread : 3
- Main thread : 3
- Main thread : 4
- Child Thread : 4
- Child thread finished!
- Main thread finished!
- */
Welcome to the world of Java examples, organized by categories and Java packages. Java examples (Java sample source code) help to understand functionality of various Java classes and methods as well as various programming techniques in a simple way, which is otherwise very hard to learn by reading tutorials or Java API. So start exploring...
Sunday, November 27, 2011
Create New Thread Using Runnable Example
Subscribe to:
Post Comments (Atom)
How to find the most appropriate Keywords?
🔍 Step 1: Understand Your Business and Audience Define your products, services, or content . Identify your target audien...
-
/* Pause Thread Using Sleep Method Example This Java example shows how to pause currently running thread using sleep method of Jav...
-
/* Java String split example. This Java String split example describes how Java String is split into multiple Java String objects. */ ...
-
// File Name GreetingClient.java import java.net.*; import java.io.*; public class GreetingClient { public static void main(String [] ...
No comments:
Post a Comment