Thread Programs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

// TestSynchronization.

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 MyThread1 extends Thread{


Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}

}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}

public class TestSynchronization{


public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}

// 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());
}

public static void main(String args[]){


TwoThreadSleep t1=new TwoThreadSleep();
System.out.println("Currently Executing The Thread : " + Thread.currentThread().getName());
n++;
try {
Thread.sleep(5000); //5000 milliseconds = 5 s
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("static value incremented to"+ n);
t1.start();
System.out.println("Awake after sleep");
}
}

// 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);

public static void main(String args[]){


MultiThreadSleep t1=new MultiThreadSleep();
MultiThreadSleep t2=new MultiThreadSleep();
MultiThreadSleep t3=new MultiThreadSleep();
MultiThreadSleep t4=new MultiThreadSleep();
MultiThreadSleep t5=new MultiThreadSleep();

System.out.println("Currently Executing The Thread : " + Thread.currentThread().getName());

t1.start();
t2.start();
t3.start();
t4.start();
t5.start();

System.out.println("Awake after sleep");


}
}

// Java program to demonstrate inter-thread communication (wait(), join() and notify())


// interthreadexample1.java
import java.util.Scanner;

public class interthreadexample1


{
public static void main(String[] args) throws InterruptedException
{
final PC pc = new PC();

// Create a thread object that calls pc.produce()


Thread t1 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.produce();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});

// Create another thread object that calls pc.consume()


Thread t2 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.consume();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});

for(int i=1;i<=10;i++)
{
// Start both threads
t1.start();
t2.start();

// t1 finishes before t2
t1.join();
t2.join();
}
}

// PC (Produce Consumer) class with produce() and consume() methods.


public static class PC
{
public static int count=0;
// Prints a string and waits for consume()
public void produce()throws InterruptedException
{
// synchronized block ensures only one thread running at a time.
synchronized(this)
{
count++;
System.out.println("producer thread running");
System.out.println("count value "+count);

// releases the lock on shared resource


wait();

// and waits till some other method invokes notify().


System.out.println("Resumed");
}
}

// 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);

// synchronized block ensures only one thread


// running at a time.
synchronized(this)
{
count--;

System.out.println("consumer thread running");


System.out.println("count value "+count);
System.out.println("Waiting for return key. press the enter key");
s.nextLine();
System.out.println("Return key pressed");

// notifies the produce thread that it can wake up.


notify();

// Sleep
Thread.sleep(2000);
}
}
}
}

//Below 2 programs with and without using Start


// ThreadWOStartMethod.java
class MyThread extends Thread {
public void run()
{
System.out.println("Current thread name: "
+ Thread.currentThread().getName());

System.out.println("run() method called");


}
}

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());

System.out.println("run() method called");


}
}
class ThreadWStartMethod {
public static void main(String[] args)
{
MyThread t = new MyThread();
t.start();
}
}

class MyThread implements Runnable {


Thread td;
volatile boolean suspended;
volatile boolean stopped;

MyThread(String name) {
td = new Thread(this, name);
suspended = false;
stopped = false;
td.start();
}

public void run() {


System.out.println(td.getName() + " starting.");
try {
for (int i = 1; i < 1000 && !stopped; i++) {
System.out.print(i + " ");
if (i % 10 == 0) {
System.out.println();
Thread.sleep(250);
}
synchronized (this) {
while (suspended) {
wait();
}
}
}
} catch (InterruptedException e) {
System.out.println(td.getName() + " interrupted.");
}
System.out.println(td.getName() + " exiting.");
}

synchronized void stop() {


stopped = true;
suspended = false;
notify();
}

synchronized void suspendThread() {


suspended = true;
}

synchronized void resumeThread() {


suspended = false;
notify();
}
}

public class SuspendAndResumeThread {


public static void main(String args[]) throws InterruptedException {
MyThread thread1 = new MyThread("Thread 1");
MyThread thread2 = new MyThread("Thread 2");

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());
}
}

You might also like