0% found this document useful (0 votes)
59 views

Java Thread Exercises

The document demonstrates the creation of multiple threads using both the Runnable interface and extending the Thread class in Java. Three classes ThreadX, ThreadY, and ThreadZ are defined that implement the Runnable interface and contain the run logic for each thread. A ThreadId class is also defined that extends Thread. The main method creates 8 ThreadId objects and starts each one. It also creates Thread objects from the Runnable classes and starts those threads.

Uploaded by

Mary Grace
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Java Thread Exercises

The document demonstrates the creation of multiple threads using both the Runnable interface and extending the Thread class in Java. Three classes ThreadX, ThreadY, and ThreadZ are defined that implement the Runnable interface and contain the run logic for each thread. A ThreadId class is also defined that extends Thread. The main method creates 8 ThreadId objects and starts each one. It also creates Thread objects from the Runnable classes and starts those threads.

Uploaded by

Mary Grace
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Illustration 6.

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


Illustration 6.3

/* Demonstration of thread class methods : getID() */


/* Java code for thread creation by extending the Thread class */

class ThreadId extends Thread {


public void run() {
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


try {
// Displaying the thread that is running
System.out.println ("Thread " + Thread.currentThread().getId() + " is
running");
}
catch (Exception e) {
// Throwing an exception
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


System.out.println ("Exception is caught");
}
}
}

public class Demonstration_113{


public static void main(String[] args)
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


{
int n = 8; // Number of threads
for (int i=0; i<8; i++)
{
ThreadId object = new ThreadId();
object.start();
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
}
Illustration 6.4

/* Demonstration of thread class methods : getID() */


/* Java code for thread creation by implementing the Runnable Interface */
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


class ThreadId implements Runnable
{
public void run() {
try {
// Displaying the thread that is running
System.out.println ("Thread " + Thread.currentThread().getId() + " is
running");
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
catch (Exception e)
{
// Throwing an exception
System.out.println ("Exception is caught");
}
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}

// Main Class
class Demonstration_114 {
public static void main(String[] args)
{
int n = 8; // Number of threads
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


for (int i=0; i<8; i++){
Thread object = new Thread(new ThreadId());
object.start();
}
}
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


Illustration 6.5

/* Use of yield(), stop() and sleep() methods */

class ClassA extends Thread{


public void run() {
System.out.println("Start Thread A ....");
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


for(int i = 1; i <= 5; i++) {
if (i==1) yield();
System.out.println("From Thread A: i = "+ i);
}
System.out.println("... Exit Thread A");
}
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {

class ClassB extends Thread{


public void run() {
System.out.println("Start Thread B ....");
for(int j = 1; j <= 5; j++) {
System.out.println("From Thread B: j = "+ j);
if (j==2) stop();
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
System.out.println("... Exit Thread B");
}
}

class ClassC extends Thread{


public void run() {
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


System.out.println("Start Thread C ....");
for(int k = 1; k <= 5; k++) {
System.out.println("From Thread B: j = "+ k);
if (k==3){
try{
sleep(1000);
}catch(Exception e){}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
}
System.out.println("... Exit Thread C");
}
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


public class Demonstration_115{
public static void main (String args[]) {
ClassA t1 = new ClassA();
ClassB t2 = new ClassB();
ClassC t3 = new ClassC();
t1.start(); t2.start(); t3.start();
System.out.println("... End of executuion ");
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
}
Illustration 6.6

/* Use of suspend() and resume() methods */

class Thread1 extends Thread {


Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


public void run( ) {
try{
System.out.println (" First thread starts running" );
sleep(10000);
System.out.println (" First thread finishes running" );
}
catch(Exception e){ }
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
}

class Thread2 extends Thread {


public void run( ) {
try{
System.out.println ( "Second thread starts running");
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


System.out.println ( "Second thread is suspended itself ");
suspend( );
System.out.println (" Second thread runs again" );
}
catch(Exception e){ }
}
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {

class Demonstration_116{
public static void main (String args[ ] ){
try{
Thread1 first = new Thread1( ); // It is a newborn thread
i.e. in Newborn state
Thread2 second= new Thread2( ); // another new born thread
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {

first.start( ); // first is scheduled for running


second.start( ); // second is scheduled for running

System.out.println("Revive the second thread" ); // If it


is suspended
second.resume( );
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {

System.out.println ("Second thread went for 10 seconds sleep


" );
second.sleep (10000);

System.out.println ("Wake up second thread and finishes


running" );
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


System.out.println ( " Demonstration is finished ");
}
catch(Exception e){ }
}
}
Illustration 6.7 // Status information of threads //
/* Setting priority to threads */
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {

class ClassA extends Thread{


public void run() {
System.out.println("Start Thread A ....");
for(int i = 1; i <= 5; i++) {
System.out.println("From Thread A: i = "+ i);
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


System.out.println("... Exit Thread A");
}
}

class ClassB extends Thread{


public void run() {
System.out.println("Start Thread B ....");
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


for(int j = 1; j <= 5; j++) {
System.out.println("From Thread B: j = "+ j);
}
System.out.println("... Exit Thread B");
}
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


class ClassC extends Thread{
public void run() {
System.out.println("Start Thread C ....");
for(int k = 1; k <= 5; k++) {
System.out.println("From Thread B: j = "+ k);
}
System.out.println("... Exit Thread C");
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
}

class Demonstration_117{
public static void main (String args[]) {
ThreadA t1 = new ThreadA();
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


ThreadB t2 = new ThreadB();
ThreadC t3 = new ThreadC();

t3.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(t2.getPriority() + 1);
t1.setPriority(Thread.MIN_PRIORITY);
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


t1.start(); t2.start(); t3.start();
System.out.println("... End of executuion ");
}
}
Illustration 6.7 // Status information of threads //
/* Setting priority to threads */
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


class ClassA extends Thread{
public void run() {
System.out.println("Start Thread A ....");
for(int i = 1; i <= 5; i++) {
System.out.println("From Thread A: i = "+ i);
}
System.out.println("... Exit Thread A");
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
}

class ClassB extends Thread{


public void run() {
System.out.println("Start Thread B ....");
for(int j = 1; j <= 5; j++) {
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


System.out.println("From Thread B: j = "+ j);
}
System.out.println("... Exit Thread B");
}
}

class ClassC extends Thread{


Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


public void run() {
System.out.println("Start Thread C ....");
for(int k = 1; k <= 5; k++) {
System.out.println("From Thread B: j = "+ k);
}
System.out.println("... Exit Thread C");
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}

class Demonstration_117{
public static void main (String args[]) {
ThreadA t1 = new ThreadA();
ThreadB t2 = new ThreadB();
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


ThreadC t3 = new ThreadC();

t3.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(t2.getPriority() + 1);
t1.setPriority(Thread.MIN_PRIORITY);

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


Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


System.out.println("... End of executuion ");
}
}
Illustration 6.8
/* Data race example. */

public class Demonstration_118 extends Thread {


Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


public static int x;
public void run() {
for (int i = 0; i < 100; i++) {
x = x + 1;
x = x - 1;
}
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


public static void main(String[] args) {
x = 0;
for (int i = 0; i < 1000; i++){
new Demonstration_118().start();
System.out.println(x); // x not always 0!
}
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
Illustration 6.9
/* The following Java application shows how the transactions in a bank can be carried
out concurrently. */
class Account {
public int balance;
public int accountNo;
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


void displayBalance() {
System.out.println("Account No:" + accountNo + "Balance: " +
balance);
}

synchronized void deposit(int amount){


balance = balance + amount;
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


System.out.println( amount + " is deposited");
displayBalance();
}

synchronized void withdraw(int amount){


balance = balance - amount;
System.out.println( amount + " is withdrawn");
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


displayBalance();
}
}

class TransactionDeposit implements Runnable{


int amount;
Account accountX;
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


TransactionDeposit(Account x, int amount){
accountX = x;
this.amount = amount;
new Thread(this).start();
}

public void run(){


Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


accountX.deposit(amount);
}
}

class TransactionWithdraw implements Runnable{


int amount;
Account accountY;
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {

TransactionWithdraw(Account y, int amount) {


accountY = y;
this.amount = amount;
new Thread(this).start();
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


public void run(){
accountY.withdraw(amount);
}
}

class Demonstration_119{
public static void main(String args[]) {
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


Account ABC = new Account();
ABC.balance = 1000;
ABC.accountNo = 111;
TransactionDeposit t1;
TransactionWithdraw t2;
t1 = new TransactionDeposit(ABC, 500);
t2 = new TransactionWithdraw(ABC,900);
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
}
Illustration 6.10 : // Inter thread communication : Producer & Consumer
problem //

class Q { // Q is a class containing two parallel


processes
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


int n;
boolean flag = false;
//PRODUCER
synchronized void put( int n) { // Produce a value
if(flag) {
// Entry
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


try wait( ); catch(InterruptedException e);
// to the
} /
/ critical section

this.n = n;
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


System.out.println( "Produce :" + n);
// Critical Section

flag = true;
// Exit from the
notify( );
// critical section
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}
//CONSUMER
synchronized int get( ) { // Consume a value
if(! flag) {
// Entry
try wait( ); catch(InterruptedException e);
// to the
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


} /
/ critical section

System.out.println( "Consume :" + n);


// Critical Section
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


flag = false;
// Exit from the
notify( );
// critical // section
return( n );
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {

class Producer implement Runnable { // Thread for Producer process


Q q;
Producer ( Q q ) { // constructor
this.q =q;
new thread (this).start ( ) ; // Producer process is
started
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}

public void run( ) { // infinite running thread for Producer


int i = 0;
while (true )
q.put ( i++ );
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}

class Consumer implement Runnable { // Thread for consumer process


Q q;
Consumer (Q q ) { // Constructor
this.q = q;
new Thread (this).start ( );
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


}

public void run( ) { // infinite running thread for Consumer


while (true)
q.get ( );
}
}
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {

class PandC {
public static void main( String args[ ] ) {
Q q = new Q( ); // an instance of parallel processes is
created
new Producer(q) ; // Run the thread for
producer
Illustration 6.2

/* Creating three threads using the Runnable interface and then running them
concurrently. */
class ThreadX implements Runnable{
public void run( ) {
for(int i = 1; i <= 5; i++) {
System.out.println("Thread X with i = "+ -1*i);
}
System.out.println("Exiting Thread X ...");
}
}

class ThreadY implements Runnable {


public void run( ) {
for(int j = 1; j <= 5; j++) {
System.out.println("Thread Y with j = "+ 2*j);
}
System.out.println("Exiting Thread Y ...");
}
}
class ThreadZ implements Runnable{
public void run( ) {
for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ (2*k-1));
}
System.out.println("Exiting Thread Z ...");
}
}

public class Demonstration_112 {


new Consumer (q); // Run consumer thread
}
}

You might also like