Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C Language that has two threads - creating thread (thread A)

ID: 3598222 • Letter: W

Question

Write a program in C Language that has two threads - creating thread (thread A) and created thread (thread B).

After creating thread B, thread A waits for thread B to terminate and then it terminates itself. Thread B terminates only under a special circumstance described later. Signal SIGINT (ctrl C) is blocked in thread B (i.e. only thread A handles SIGINT).

Every time, the user presses ctrl C, thread A tries to cancel thread B. Thread B keeps pending any cancellation request received during the first 100 seconds of its life.

Any cancellation request is honored once the first 100 seconds of the thread B's life are over. Also, thread B prints its current lifetime in terms of seconds after every 10 seconds.

Explanation / Answer

class ThreadTest1

{

    public static void main(String args[])

    {

        MyThread thread1 = new MyThread("thread1: ");

        MyThread thread2 = new MyThread("thread2: ");

        thread1.start();

        thread2.start();

        boolean thread1IsAlive = true;

        boolean thread2IsAlive = true;

        do {

           if (thread1IsAlive && !thread1.isAlive()) {

               thread1IsAlive = false;

System.out.println("Thread 1 is dead.");

           }

           if (thread2IsAlive && !thread2.isAlive()) {

               thread2IsAlive = false;

               System.out.println("Thread 2 is dead.");

           }

        } while(thread1IsAlive || thread2IsAlive);

    }

}

class MyThread extends Thread

{

static String message[] =

{ "Java", "is", "hot,", "aromatic,", "and", "invigorating."};

    public MyThread(String id)

    {

        super(id);

    }

    public void run()

    {

        String name = getName();

        for (int i=0;i<message.length;++i) {

           randomWait();

           System.out.println(name + message[i]);

        }

    }

    void randomWait()

    {

        try {

           sleep((long)(3000*Math.random()));

        } catch (InterruptedException x) {

           System.out.println("Interrupted!");

        }

    }

}

This program creates two threads of execution, thread1 and thread2, from the MyThread class. It then starts both threads and executes a do statement that waits for the threads to die. The threads display the Java is hot, aromatic, and invigorating. message word by word, while waiting a short, random amount of time between each word. Because both threads share the console window, the program's output identifies which threads were able to write to the console at various times during the program's execution.

Run ThreadTest1 to get an idea of the output that it produces. Each time you run the program you might get a different program display. This is because the program uses a random number generator to determine how long each thread should wait before displaying its output. Look at the following output:

C:javajdgch08>java ThreadTest1
thread1: Java
thread2: Java
thread2: is
thread2: hot,
thread2: aromatic,
thread1: is
thread1: hot,
thread2: and
thread1: aromatic,
thread1: and
thread2: invigorating.
Thread 2 is dead.
thread1: invigorating.
Thread 1 is dead.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote