Java Thread

  1. Create a Simple Thread in Java
  2. Create a Runnable Thread in Java
  3. join() & isAlive() using Thread in Java
  4. Call sleep() using Thread in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Create a Simple Thread in Java


Thread_Ex1.java

public class Thread_Ex1 extends Thread{
 
	String text = null;
	
	Thread_Ex1(String str) {
		
		text = str;
		start();
	}
	
	public void run() {
		
		System.out.println(text);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		new Thread_Ex1("Thread Activity Started..");
	}
}

Sample Output

Thread Activity Started..




B. Create a Runnable Thread in Java


Thread_Ex2.java

public class Thread_Ex2 implements Runnable{
 
	String text = null;
	Thread thread;
	
	Thread_Ex2(String str) {
		
		text = str;
		thread = new Thread(this);
		thread.start();
	}
	
	public void run() {
		
		System.out.println(text);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		new Thread_Ex2("Thread Activity Started..");
	}
}

Sample Output

Thread Activity Started..




C. join() & isAlive() using Thread in Java


Thread_Ex3.java

public class Thread_Ex3 extends Thread{
 
	String text = null;
	int count;
	
	Thread_Ex3(String str) {
		
		super(str);
		count = 0;
		start();
	}
	
	public void run() {
		
		while(count < 3) {
			
			System.out.println(getName() + " - count : " + count);
			count ++;
			
			try {
				
				Thread.sleep(250);
			} 
			catch (InterruptedException e) {
				
				e.printStackTrace();
			}  
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Thread_Ex3 obj1 = new Thread_Ex3("Thread A");
		Thread_Ex3 obj2 = new Thread_Ex3("Thread B");
		
		try {
			
			obj1.join();
			
			if (!obj1.isAlive())
				System.out.println("Thread A not alive.");
 
			obj2.join();
 
			if (!obj2.isAlive())
				System.out.println("Thread B not alive.");
		} catch (Exception e) { }
		
		System.out.println("Exit from Main Thread.");
	}
}

Sample Output

Thread A - count : 0
Thread B - count : 0
Thread A - count : 1
Thread B - count : 1
Thread A - count : 2
Thread B - count : 2
Thread A not alive.
Thread B not alive.
Exit from Main Thread.




D. Call sleep() using Thread in Java


Thread_Ex4.java

import java.util.Date;
 
public class Thread_Ex4 {
 
	int wait = 0;
	
	Date dt;
	
	void ThreadActivity() {
		
		dt = new Date();
		System.out.println("Before Threading");
		System.out.println(dt.getHours() + " : " 
				+ dt.getMinutes() + " : " 
				+ dt.getSeconds());
 
		try {
			while(wait <= 100) {
				Thread.sleep(100);
				wait ++;
			}
		}
		catch (Exception e) {
			
			System.out.println("Error : " + e.toString());
		}
		finally {
			
			dt = new Date();
			System.out.println("\nAfter Threading");
			System.out.println(dt.getHours() + " : " 
					+ dt.getMinutes() + " : " 
					+ dt.getSeconds());
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Thread_Ex4 obj = new Thread_Ex4();
		
		obj.ThreadActivity();
	}
}

Sample Output

Before Threading
14 : 33 : 12

After Threading
14 : 33 : 22






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com