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

Java Programming Chapter 22: How to work with threads PROBLEM 1. Given a class n

ID: 3575029 • Letter: J

Question

Java Programming

Chapter 22: How to work with threads

PROBLEM

     1.   Given a class named MonitorThread that extends the Thread class, write one or more statements that create an instance of the MonitorThread class, assign it to a variable named monitor, and start the thread.

     2.   Given a class named Monitor that implements the Runnable interface, write one or more statements that starts the Monitor object as a thread. The code should include a declaration for a variable named monitor that will hold the Thread object.

Code example 22-1

public class CountDownApp

{

    public static void main(String[] args)

    {

        Thread t = new CountDown();

        t.start();

    }

}

public class CountDown extends Thread

{

    @Override

    public void run()

    {

        for (int i = 10; i > 0; i--)

        {

            System.out.println(this.getName() + " Count " + i);

        }

    }

}

     3.   (Refer to code example 22-1.) Modify the program so the Count class implements the Runnable interface instead of extending the Thread class.

     4.   (Refer to code example 22-1.) Modify the run method of the CountDown class so that the count down occurs at approximately one second intervals.

     5.   Write a statement that causes the current thread to pause for at least 5 seconds.

     6.   Given a class named MonitorThread that extends the Thread class, write code to create an instance of this thread, set its priority to the lowest possible setting, and start the thread.

     7.   Write the code for a class named TickThread that extends the Thread class. The run method for this class should display the message “Tick” on the console at approximately one second intervals until the thread is interrupted. The method should use both isInterrupted() and InterruptedException to find out if it has been interrupted.

     8.   Write the main method for an application that creates and starts a thread named from a class named TickThread (assume this class extends Thread), pauses for five seconds, then interrupts the TickThread thread.

     9.   Write the code for a public class named SequenceNumber that has a single method named getSequenceNumber. This method should return an integer value that increases by 1 each time the method is called. The first time the method is called, it should return 1. The method should be written so that it can safely be called by multiple threads.

   10.   Write a while loop that causes the current thread to wait until a class variable named accountBalance has a value that’s greater than zero.

Explanation / Answer

These all converges t multithreading

I will answer point-wise:( upto 4 subpoints as we do answer )

1. Its like this:

class MonitorThread extends Thread{

//...some members

//implement run method

public void run()

{//code

}

psvm(..) // main function

{

MonitorThread monitor=new MonitorThread();

monitor.start(); //will call run method

}

}

2.

//rest is same:

use this is main:

3.

//rest same

public class CountDown implements Runnable{

//rest same run() method only is verridded

4.

After this line " System.out.println(this.getName() + " Count " + i);"

write: Thread.sleep(1000); // 100 ms = 1 s

5.

In start of run method:

write:Thread.currentThread().sleep(5000);

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