0% found this document useful (0 votes)
2 views29 pages

Module5 Jjj

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 29

Rashtreeya Sikshana Samithi Trust

RV Institute of Technology and Management®


(Affiliated to VTU, Belagavi)

JP Nagar, Bengaluru – 560076

Department of Computer Science and Engineering

Course Name: Object Oriented Programming with Java


Course Code: BCS306A
III Semester 2022 Scheme
Prepared By:

Prof Nandita and Prof Syeda Ayesha


Assistant Professor
Department of Computer Science and Engineering,RVITM,
Bengaluru – 560076

Email: nanditab.rvitm@rvei.edu.in
syedaayesha.rvitm@rvei.edu.in
RV Institute of Technology and Management ®

MODULE 5 :Multi-Threaded Programming


Thread:

➢ A thread as shown in Fig. 5.1 is a lightweight sub process, a smallest unit


ofprocessing.
➢ It is a separate path of execution.
➢ Threads are independent, if there occurs exception in one thread, it doesn'taffect
other threads.

➢ It shares a common memory area.

t1, t2, t3 are


threads

Fig. 5.1: Threads

Java provides built-in support for multithreaded programming. The process of


executing multiple threads simultaneously is known as multithreading. A multithreaded
programcontains two or more parts that can run concurrently. Each part of such a program
is called athread, and each thread defines a separate path of execution. Thus, multithreading
is aspecialized form of multitasking.

Multitasking
Multitasking is a process of executing multiple tasks simultaneously. We use multitasking
toutilize the CPU. Multitasking can be achieved by two ways:

• Process-based Multitasking (Multiprocessing)


• Thread-based Multitasking (Multithreading)

III- Semester, Object Oriented Programming with JAVA Page 2 of 29


(BCS306A)
RV Institute of Technology and Management ®

1) Process-based Multitasking (Multiprocessing)

• Each process has its own address in memory i.e. each process allocates separate
memory area.
• Process is heavyweight.
• Cost of communication between the processes is high.

• Switching from one process to another require some time for saving and loading
registers, memory maps, updating lists etc.
• Less efficient

2) Thread-based Multitasking (Multithreading)


• Threads share the same address space.
• Thread is lightweight.
• Cost of communication between the threads is low.
• Highly efficient

Multithreading has several advantages over Multiprocessing such as;


• Threads are lightweight compared to processes
• Threads share the same address space and therefore can share both data and code.
• Context switching between threads is usually less expensive than between processes

• Cost of thread intercommunication is relatively low that that of process


intercommunication

• Threads allow different tasks to be performed concurrently.

An instance of Thread class is just an object, like any other object in java. But a thread of
execution means an individual "lightweight" process that has its own call stack. In java each
thread has its own call stack as shown in Fig. 5.2.

Fig. 5.2: thread

III- Semester, Object Oriented Programming with JAVA Page 3 of 29


(BCS306A)
RV Institute of Technology and Management ®

The Main Thread


When a Java program starts up, one thread begins running immediately. This is usually called
themain thread of your program, because it is the one that is executed when your program
begins. The main thread is important for two reasons:

• It is the thread from which other ―child‖ threads will be spawned.

• Often, it must be the last thread to finish execution because it performs various shutdown
actions.

Fig. 5.3: Life cycle of a Thread (Thread States)

The life cycle of the thread is shown in Fig. 5.3 in java is controlled by JVM. The java thread
states are as follows:
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated

III- Semester, Object Oriented Programming with JAVA Page 4 of 29


(BCS306A)
RV Institute of Technology and Management ®

1. New:
The thread is in new state if you create an instance of Thread class but
before the invocation ofstart () method.

2. Runnable:
When we call start () function on Thread object, its state is changed to Runnable.
The control is given to Thread scheduler to finish its execution. Whether to run this
thread instantly or keep it in runnable thread pool before running, depends on the
OS implementation of thread scheduler.

a) Running:
When thread is executing, it’s state is changed to Running. Thread scheduler picks one
of the thread from the runnable thread pool and change its state to Running. Then CPU
starts executingthis thread. A thread can change state to Runnable, Dead or Blocked
from running state depends on time slicing, thread completion of run() method or
waiting for some resources.

b) Non-Runnable (Blocked)

This is the state when the thread is still alive, but is currently not eligible to run. Waiting
− Sometimes, a thread transitions to the waiting state while the thread waits for another
thread to perform a task. A thread transitions back to the runnable state, only when
another thread signals the waiting thread to continue its execution.

c) Terminated

A thread is in terminated or dead state when its run() method exits.

Thread Creation

When the thread starts it schedules the time slots for several sub threads and the JVM scheduler
schedules the time slot to every thread based on round robin technique or priority based.

Method Description

String getName() Retrieves the name of running thread in the current context in String
format

III- Semester, Object Oriented Programming with JAVA Page 5 of 29


(BCS306A)
RV Institute of Technology and Management ®

void start() This method will start a new thread of execution by calling run() method
of Thread/runnable object.

void run() This method is the entry point of the thread. Execution of thread starts
from this method.

void sleep(int This method suspends the thread for mentioned time duration in
argument
sleeptime) (sleeptime in ms)

void yield() By invoking this method, the current thread pauses its execution
temporarily and allow other threads to execute.

void join() This method used to queue up a thread in execution. Once called on
thread, current thread will wait till calling thread completes its
execution

There are two ways to create thread in java;

• Implement the Runnable interface (java.lang.Runnable)


• By Extending the Thread class (java.lang.Thread)

Extending Thread class


Creates a thread by a new class that extends Thread class. This creates an instance of that class.
The extending class must override run() method which is the entry point of new thread

class Multi extends Thread{


public void run(){
System.out.println("thread is running...");
}

public static void main(String args[]){


Multi t1=new Multi();
t1.start();

Output: thread is running

III- Semester, Object Oriented Programming with JAVA Page 6 of 29


(BCS306A)
RV Institute of Technology and Management ®

Implementing the Runnable Interface


The easiest way to create a thread is to create a class that implements the runnable
interface.After implementing runnable interface, the class needs to implement the run ()
method, which is of form,

public void run ()

• run () method introduces a concurrent thread into your program. This thread will end
whenrun() returns.
• You must specify the code for your thread inside run() method.

• run() method can call other methods, can use other classes and declare variables just
like anyother normal method.

After you create a class that implements Runnable, you will instantiate an object of type
Thread from within that class. Thread defines several constructors. The one that we will use
is shown here:

Syntax :
Thread(Runnable threadOb, String threadName);

In this constructor, threadOb is an instance of a class that implements the Runnable interface.
This defines where execution of the thread will begin. The name of the new thread is
specified by threadName.

Ex : Thread t = new Thread(mt);

class MyThread implements Runnable


{
public void run()

System.out.println("concurrent thread started running..");


}
}
class MyThreadDemo
{
public static void main( String args[] )

III- Semester, Object Oriented Programming with JAVA Page 7 of 29


(BCS306A)
RV Institute of Technology and Management ®

{
MyThread mt = new MyThread();
Thread t = new Thread(mt); t.start();
}

Output: concurrent thread started running..

Creating Multiple Threads by implementing Runnable Interface

package applet1;
import java.io.*;
import java.util.*;

class A implements Runnable {


public void run() {
try {
for (int i = 5; i > 0; i--)
{
System.out.println(i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println( "Interrupted");
}
System.out.println(" exiting.");
}
}
class B implements Runnable {

public void
run() {try {

III- Semester, Object Oriented Programming with JAVA Page 8 of 29


(BCS306A)
RV Institute of Technology and Management ®

for (int i = 15; i > 10; i--) {


System.out.println(i);
Thread.sleep(1000);
}

} catch (InterruptedException e) {
System.out.println("Interrupted")
;

System.out.println(" exiting.");
}
}
class multithread {

public static void main(String args[])

{A obj1 = new A();


B obj2= new B();
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
t1.start();
t2.start();
}}

Output: 5
15
14

4
13
3
12
2

III- Semester, Object Oriented Programming with JAVA Page 9 of 29


(BCS306A)
RV Institute of Technology and Management ®

11

exiting
exiting

Notice the call to sleep (10000) in main (). This causes threads B and A to sleep for ten
secondsand ensures that it will finish last.

Note: Write a java application program for generating 3 threads to perform thefollowing
operations.

i) Reading n numbers ii) Printing prime numbers iii) Computing average of nnumbers

isAlive() and join() methods


In java, isAlive() and join() are two different methods to check whether a thread has finished
itsexecution.

The isAlive() method returns true if the thread upon which it is called is still running
otherwise itreturns false.

final boolean isAlive()


public class MyThread extends Thread
{
public void run()

{
System.out.println("r1 ");
try {
Thread.sleep(500);
}
catch(InterruptedException ie)

{ }System.out.println("r2 ");
}
public static void main(String[] args)

III- Semester, Object Oriented Programming with JAVA Page 10 of 29


(BCS306A)
RV Institute of Technology and Management ®

MyThread t1=new MyThread();


MyThread t2=new MyThread();

t1.start();
t2.start();
System.out.println(t1.isAlive());
System.out.println(t2.isAlive());
}
}

Output:
r1
true
true
r1
r2
r2

But, join() method is used more commonly than isAlive(). This method waits until the thread
onwhich it is called terminates

final void join() throws InterruptedException

Using join() method, we tell our thread to wait until the specified thread completes its
execution. There are overloaded versions of join() method, which allows us to specify time for
which you want to wait for the specified thread to terminate

public class MyThread extends Thread


{
public void run()
{

System.out.println("r1 ");
try {
Thread.sleep(500);
}catch(InterruptedException ie){ }
System.out.println("r2 ");

III- Semester, Object Oriented Programming with JAVA Page 11 of 29


(BCS306A)
RV Institute of Technology and Management ®

public static void main(String[] args)


{
MyThread t1=new MyThread();

MyThread t2=new MyThread();


t1.start();
try{

t1.join(); //Waiting for t1 to finish

}catch(InterruptedException ie){}

t2.start();
}
}

Output:

r1
r2
r1
r2

In this above program join() method on thread t1 ensures that t1 finishes it process before
threadt2 starts.

Specifying time with join()


If in the above program, we specify time while using join() with t1, then t1 will execute for
thattime, and then t2 will join it.

t1.join(1500);

III- Semester, Object Oriented Programming with JAVA Page 12 of 29


(BCS306A)
RV Institute of Technology and Management ®

Synchronization
At times when more than one thread try to access a shared resource, we need to ensure that
resource will be used by only one thread at a time. The process by which this is achieved is

called synchronization. The synchronization keyword in java creates a block of code referred
toas critical section.

Key to synchronization is the concept of the monitor. A monitor is an object that is


usedas a mutually exclusive lock. Only one thread can own a monitor at a given time. When
a thread acquires a lock, it is said to have entered the monitor. All other threads attempting
to enter the locked monitor will be suspended until the first thread exits the monitor. These
other threads are said to be waiting for the monitor.

Why use Synchronization


The synchronization is mainly used to

• To prevent thread interference.


• To prevent consistency problem.

Thread Synchronization & Mutual Exclusive


There are two types of thread synchronization mutual exclusive and inter-thread
communication.
1. Mutual Exclusive
1. Synchronized method.
2. Synchronized block.
3. static synchronization.

Using Synchronized Blocks


Synchronized block can be used to perform synchronization on any specific resource of the
method. Synchronized block is used to lock an object for any shared resource. Scope of
synchronized block is smaller than the method.

General Syntax :
synchronized (object)

//statement to be synchronized
}

III- Semester, Object Oriented Programming with JAVA Page 13 of 29


(BCS306A)
RV Institute of Technology and Management ®

class Table{
void display(int n)
{
synchronized(this)
{
for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try{
Thread.sleep(400);

catch(Exception e){
System.out.println(e);
}
}
}
}//end of the method

class A extends Thread


{
Table t;
A(Table t)
{
this.t=t;
}
public void run(){
t.display(5);
}
}
class B extends Thread{
Table t;
B(Table t)

III- Semester, Object Oriented Programming with JAVA Page 14 of 29


(BCS306A)
RV Institute of Technology and Management ®

this.t=t;
}
public void run(){

t.display(100);
}
}

public class synch


{
public static void main(String args[])
{
Table obj = new Table(); //only one object

A t1=new A(obj);
B t2=new B(obj);
t1.start();
t2.start();

}
}
Output:
5

10
15
20
25
100
200
300
400
500

III- Semester, Object Oriented Programming with JAVA Page 15 of 29


(BCS306A)
RV Institute of Technology and Management ®

Using Synchronized Methods


If you declare any method as synchronized, it is known as synchronized method.
Synchronized method is used to lock an object for any shared resource. When a thread
invokes a synchronized method, it automatically acquires the lock for that object and
releases it when the thread completes its task.

class Table{
synchronized void display(int n)
{
for(int i=1;i<=5;i++)

{
System.out.println(n*i);
try{
Thread.sleep(400);
}
catch(Exception e){
System.out.println(e);
}

}
}
}//end of the method

class A extends Thread


{
Table t;
A(Table t)
{
this.t=t;
}
public void run(){
t.display(5);
}
}

III- Semester, Object Oriented Programming with JAVA Page 16 of 29


(BCS306A)
RV Institute of Technology and Management ®

class B extends Thread{


Table t;
B(Table t){

this.t=t;
}

public void run(){

t.display(100);
}
}

public class synch


{
public static void main(String args[])
{
Table obj = new Table(); //only one object
A t1=new A(obj);

B t2=new B(obj);
t1.start();
t2.start();
}
}
Output:
5
10
15

20
25
100
200
300
400

III- Semester, Object Oriented Programming with JAVA Page 17 of 29


(BCS306A)
RV Institute of Technology and Management ®

In computing, the producer–consumer problem (also known as the bounded-buffer problem) is


a classic example of a multi-process synchronization problem. The problem describes two
processes, the producer and the consumer, which share a common, fixed-size buffer used as a
queue.
• The producer’s job is to generate data, put it into the buffer, and start again.

• At the same time, the consumer is consuming the data (i.e. removing it from the
buffer), one piece at a time.

Solution:
This problem can be implemented or solved by different ways in Java, classical way is
using waitand notify method to communicate between Producer and Consumer thread and
blocking each of themon individual condition like full queue and empty queue.

wait ( ) tells the calling thread to give up the monitor and go to sleep until some other
threadenters the same monitor and calls notify( ) or notifyAll( ).

• notify( ) wakes up a thread that called wait( ) on the same object.


• notifyAll( ) wakes up all the threads that called wait( ) on the same object. One of the
threadswill be granted access.

These methods are declared within Object, as shown here:


• final void wait( ) throws InterruptedException
• final void notify( )
• final void notify All( )

III- Semester, Object Oriented Programming with JAVA Page 18 of 29


(BCS306A)
RV Institute of Technology and Management ®

public class
ProducerConsumerTest
{ public static void
main(String[] args) {
Myclass c = new Myclass();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
class Myclass {
private int contents;
private boolean available = false;

public synchronized int get() {


while (available == false) {
try {
wait();
} catch (InterruptedException e) {}
}

available = false;
notifyAll();

return contents;
}

public synchronized void put(int value) {


while (available == true) {
try {

wait();
} catch (InterruptedException e) { }
}
contents = value;

III- Semester, Object Oriented Programming with JAVA Page 19 of 29


(BCS306A)
RV Institute of Technology and Management ®

available = true;
notifyAll();

class Consumer extends Thread {


private Myclass Myclass;
private int number;

public Consumer(Myclass c, int number) {


Myclass = c;

this.number = number;

public void run() {


int value = 0;

for (int i = 0; i < 10; i++) {


value = Myclass.get();
System.out.println("Consumer #" + this.number + " got: " + value);
}
}
}

class Producer extends Thread {


private Myclass Myclass;
private int number;
public Producer(Myclass c, int number) {
Myclass = c;
this.number = number;

}
public void run() {

III- Semester, Object Oriented Programming with JAVA Page 20 of 29


(BCS306A)
RV Institute of Technology and Management ®

for (int i = 0; i < 10; i++)


{Myclass.put(i);
System.out.println("Producer #" + this.number + " put: " + i); try
{
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}

Output:

Producer #1 put: 0

Consumer #1 got: 0

Producer #1 put: 1

Consumer #1 got: 1

Producer #1 put: 2

Consumer #1 got: 2
Producer #1 put: 3

Consumer #1 got: 3

Producer #1 put: 4

Consumer #1 got: 4

Producer #1 put: 5

Consumer #1 got: 5

Producer #1 put: 6

Consumer #1 got: 6

Producer #1 put: 7

Consumer #1 got: 7

III- Semester, Object Oriented Programming with JAVA Page 21 of 29


(BCS306A)
RV Institute of Technology and Management ®

Producer #1 put: 8

Consumer #1 got: 8

Producer #1 put: 9

Consumer #1 got: 9

Bounded Buffer Problem


Bounded buffer problem, which is also called producer consumer problem, is one of the
classic problems of synchronization.

Problem Statement:
There is a buffer of n slots and each slot is capable of storing one unit of data. There are
two processes running, namely, producer and consumer, which are operating on the buffer.

A producer tries to insert data into an empty slot of the buffer. A consumer tries to remove
data from a filled slot in the buffer. As you might have guessed by now, those two processes
won’t produce the expected output if they are being executed concurrently.

There needs to be a way to make the producer and consumer work in an independent manner.

import java.io.*;
import java.util.*;
class Buffer
{
private final int MaxBuffSize;
private int[] store;
private int BufferStart, BufferEnd, BufferSize;
public Buffer(int size)
{
MaxBuffSize = size;
BufferEnd = -1;

III- Semester, Object Oriented Programming with JAVA Page 22 of 29


(BCS306A)
RV Institute of Technology and Management ®

BufferStart = 0;
BufferSize = 0;
store = new int[MaxBuffSize];
}
public synchronized void insert(int ch)
{
try
{

while (BufferSize == MaxBuffSize) {


wait();
}
BufferEnd = (BufferEnd + 1) % MaxBuffSize;
BufferSize++;
notifyAll();
}
catch (InterruptedException e)
{
}
}

public synchronized int delete() {


int ch=0;
try {
while (BufferSize == 0) {
wait();
}
ch = store[BufferStart];
BufferStart = (BufferStart + 1) % MaxBuffSize;
BufferSize--;
notifyAll();
} catch (InterruptedException e) {
}
return ch;
}
}

class Consumer extends Thread


{private final Buffer buffer;
public Consumer(Buffer b) {
buffer = b;
}

III- Semester, Object Oriented Programming with JAVA Page 23 of 29


(BCS306A)
RV Institute of Technology and Management ®

public void run() {


while
(!Thread.currentThread().isInterrupted())
{int c = buffer.delete();
System.out.print(c);
}
}
}
class Producer extends Thread {
private final Buffer buffer;
public Producer(Buffer b)
{buffer = b;
}
public void run() {
for(int c=0;c<10;c++)
buffer.insert(c);

}
}
class boundedbuffer {
public static void main(String[] args) {
System.out.println("program starting");
Buffer buffer = new Buffer(5); // buffer has size 5
Producer prod = new Producer(buffer); Consumer
cons = new Consumer(buffer);
prod.start();
cons.interrupt();
try {
prod.join();
cons.interrupt();
} catch (InterruptedException e) {}
System.out.println("End of Program");
}
}
Output:
0
1
2
3
4
5
6
End of Program

III- Semester, Object Oriented Programming with JAVA Page 24 of 29


(BCS306A)
RV Institute of Technology and Management ®

Readers Writer Problem


Readers writer problem is another example of a classic synchronization problem. There are
manyvariants of this problem, one of which is examined below.

Problem Statement:
There is a shared resource which should be accessed by multiple processes. There are two
types of processes in this context. They are reader and writer. Any number of readers can
read from the shared resource simultaneously, but only one writer can write to the shared
resource. When awriter is writing data to the resource, no other process can access the
resource. A writer cannot write to the resource if there are non-zero number of readers
accessing the resource.

Solution:
From the above problem statement, it is evident that readers have higher priority than writer.
If a writer wants to write to the resource, it must wait until there are no readers currently
accessing that resource

Interthread Communication

Inter-thread communication or Co-operation is all about allowing synchronized threads to


communicate with each other.

Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in


its critical section and another thread is allowed to enter (or lock) in the same critical section to be
executed.It is implemented by following methods of Object class

o wait()
o notify()
o notifyAll()

1) wait() method

The wait() method causes current thread to release the lock and wait until either another thread
invokes the notify() method or the notifyAll() method for this object, or a specified amount of time
has elapsed.

The current thread must own this object's monitor, so it must be called from the synchronized
method only otherwise it will throw exception.

2) notify() method
The notify() method wakes up a single thread that is waiting on this object's monitor. If any
threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary
and occurs at the discretion of the implementation.
III- Semester, Object Oriented Programming with JAVA Page 25 of 29
(BCS306A)
RV Institute of Technology and Management ®

Syntax:

public final void notify()

3) notifyAll() method

Wakes up all threads that are waiting on this object's monitor.

Syntax:

public final void notifyAll()

Understanding the process of inter-thread communication

The point to point explanation of the above diagram is as follows:

1. Threads enter to acquire lock.


2. Lock is acquired by on thread.
3. Now thread goes to waiting state if you call wait() method on the object. Otherwise it
releases the lock and exits.
4. If you call notify() or notifyAll() method, thread moves to the notified state (runnable
state).
5. Now thread is available to acquire lock.
6. After completion of the task, thread releases the lock and exits the monitor state of the
object.

Why wait(), notify() and notifyAll() methods are defined in Object class not Thread class?

It is because they are related to lock and object has a lock.

III- Semester, Object Oriented Programming with JAVA Page 26 of 29


(BCS306A)
RV Institute of Technology and Management ®

Difference between wait and sleep?


Let's see the important differences between wait and sleep methods.

wait() sleep()

The wait() method releases the lock. The sleep() method doesn't release the lock.

It is a method of Object class It is a method of Thread class

It is the non-static method It is the static method

It should be notified by notify() or notifyAll() methods After the specified amount of time, sleep is completed.

Example of Inter Thread Communication in Java


1. class Customer{
2. int amount=10000;
3.
4. synchronized void withdraw(int amount){
5. System.out.println("going to withdraw...");
6.
7. if(this.amount<amount){
8. System.out.println("Less balance; waiting for deposit...");
9. try{wait();}catch(Exception e){}
10. }
11. this.amount-=amount;
12. System.out.println("withdraw completed...");
13. }
14.
15. synchronized void deposit(int amount){
16. System.out.println("going to deposit...");
17. this.amount+=amount;
18. System.out.println("deposit completed... ");
19. notify();
20. }
21. }
22.
23. class Test{
24. public static void main(String args[]){
25. final Customer c=new Customer();
26. new Thread(){
III- Semester, Object Oriented Programming with JAVA Page 27 of 29
(BCS306A)
RV Institute of Technology and Management ®

27. public void run(){c.withdraw(15000);}


28. }.start();
29. new Thread(){
30. public void run(){c.deposit(10000);}
31. }.start();
32.
33. }}

Output:

going to withdraw...
Less balance; waiting for deposit...
going to deposit...
deposit completed...
withdraw completed

Suspending, Resuming, and Stopping Threads

Java Thread suspend() method

The suspend() method of thread class puts the thread from running to waiting state. This method
is used if you want to stop the thread execution and start it again when a certain event occurs. This
method allows a thread to temporarily cease execution. The suspended thread can be resumed
using the resume() method.

Syntax
public final void suspend()

Example
1. public class JavaSuspendExp extends Thread
2. {
3. public void run()
4. {
5. for(int i=1; i<5; i++)
6. {
7. try
8. {
9. // thread to sleep for 500 milliseconds
10. sleep(500);

III- Semester, Object Oriented Programming with JAVA Page 28 of 29


(BCS306A)
RV Institute of Technology and Management ®

11. System.out.println(Thread.currentThread().getName());
12. }catch(InterruptedException e){System.out.println(e);}
13. System.out.println(i);
14. }
15. }
16. public static void main(String args[])
17. {
18. // creating three threads
19. JavaSuspendExp t1=new JavaSuspendExp ();
20. JavaSuspendExp t2=new JavaSuspendExp ();
21. JavaSuspendExp t3=new JavaSuspendExp ();
22. // call run() method
23. t1.start();
24. t2.start();
25. // suspend t2 thread
26. t2.suspend();
27. // call run() method
28. t3.start();
29. }
30. }
Output
Thread-0
1
Thread-2
1
Thread-0
2
Thread-2
2
Thread-0
3
Thread-2
3
Thread-0
4
Thread-2
4

III- Semester, Object Oriented Programming with JAVA Page 29 of 29


(BCS306A)

You might also like