Professional Documents
Culture Documents
Thread Programs
Thread Programs
Thread Programs
java
class Table{
void printTable(int n){//method not synchronized
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(1000);
}catch(Exception e){System.out.println(e);}
}
}
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
// MainMethodThread.java
class MainMethodThread extends Thread{
public static void main(String args[]){
System.out.println("Currently Executing The Thread : " + Thread.currentThread().getName());
}
}
// MainThreadSleepExample.java
import java.util.*;
public class MainThreadSleepExample {
public static void main(String[] args) {
System.out.println("Start of : " + Thread.currentThread().getName());
try {
Thread.sleep(5000); //5000 milliseconds = 5 s
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End of : " + Thread.currentThread().getName());
}
}
// MainAndThreadSleep.java
class MainAndThreadSleep extends Thread{
static int n=0;
public void run()
{
System.out.println("Inside the run() method");
n++;
try {
Thread.sleep(5000); //5000 milliseconds = 5 s
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("static value incremented to"+ n);
System.out.println("Currently Executing The Thread : " + Thread.currentThread().getName());
}
// MultiThreadSleep.java
class MultiThreadSleep extends Thread{
static int n=0;
public void run()
{
System.out.println("Currently Executing The Thread : " + Thread.currentThread().getName());
System.out.println("Inside the run() method");
n++;
/*try {
Thread.sleep(5000); //5000 milliseconds = 5 s
} catch (InterruptedException e) {
e.printStackTrace();
}*/
System.out.println("static value incremented to"+ n);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
for(int i=1;i<=10;i++)
{
// Start both threads
t1.start();
t2.start();
// t1 finishes before t2
t1.join();
t2.join();
}
}
// Sleeps for some time and waits for a key press. After key is pressed, it notifies
produce().
public void consume()throws InterruptedException
{
// this makes the produce thread to run first.
Thread.sleep(1000);
Scanner s = new Scanner(System.in);
// Sleep
Thread.sleep(2000);
}
}
}
}
class ThreadWOStartMethod {
public static void main(String[] args)
{
MyThread t = new MyThread();
t.run();
}
}
// ThreadWStartMethod.java
class MyThread extends Thread {
public void run()
{
System.out.println("Current thread name: "
+ Thread.currentThread().getName());
MyThread(String name) {
td = new Thread(this, name);
suspended = false;
stopped = false;
td.start();
}
Thread.sleep(1000);
thread1.suspendThread();
System.out.println("\nSuspending " + thread1.td.getName());
Thread.sleep(1000);
thread1.resumeThread();
System.out.println("\nResuming " + thread1.td.getName());
Thread.sleep(1000);
thread2.suspendThread();
System.out.println("\nSuspending " + thread2.td.getName());
Thread.sleep(1000);
thread2.resumeThread();
System.out.println("\nResuming " + thread2.td.getName());
Thread.sleep(1000);
thread1.stop();
System.out.println("\nStopping " + thread1.td.getName());
Thread.sleep(1000);
thread2.stop();
System.out.println("\nStopping " + thread2.td.getName());
}
}