JAVA Briefly explain the code in area with a /*comment*/ above it /** * A progra
ID: 3755560 • Letter: J
Question
JAVA Briefly explain the code in area with a /*comment*/ above it
/** * A program that tests your implementation that
* lists all thread groups
* - and threads within each group -
* in the JVM. */
public class CreateThreadGroups
{
public CreateThreadGroups() {
/* Comment Here */
ThreadGroup alpha = new ThreadGroup("alpha");
ThreadGroup beta = new ThreadGroup("beta");
ThreadGroup theta = new ThreadGroup(alpha, "theta");
/* Comment Here */
(new Thread(alpha, new GroupWorker())).start();
(new Thread(alpha, new GroupWorker())).start();
(new Thread(alpha, new GroupWorker())).start();
(new Thread(beta, new GroupWorker())).start();
(new Thread(theta, new GroupWorker())).start();
(new Thread(theta, new GroupWorker())).start();
}
class GroupWorker implements Runnable
{
public void run() {
while (true) {
try {
Thread.sleep(1000);
for (int i = 0; i < 1000000; i++)
;
}
catch (InterruptedException ie) {
}
}
}
}
}
Explanation / Answer
/** * A program that tests your implementation that
* lists all thread groups
* - and threads within each group -
* in the JVM. */
public class CreateThreadGroups
{
public CreateThreadGroups() {
/*
thread group is an object used to resume,suspend or interrupt multiple threads
*/
/*
Three thread groups are created alpha,beta,theta
*/
ThreadGroup alpha = new ThreadGroup("alpha");
ThreadGroup beta = new ThreadGroup("beta");
ThreadGroup theta = new ThreadGroup(alpha, "theta");
/*Thread Constructors takes two parameters
thread name and thread group
we start all the threading each thread will start concurrently with gap of 1 sec
* at that time another thread will start
* once a thread is in sleep then another thread will start
* and executes a loop over 1000000 times
*/
(new Thread(alpha, new GroupWorker())).start();
(new Thread(alpha, new GroupWorker())).start();
(new Thread(alpha, new GroupWorker())).start();
(new Thread(beta, new GroupWorker())).start();
(new Thread(theta, new GroupWorker())).start();
(new Thread(theta, new GroupWorker())).start();
}
class GroupWorker implements Runnable
{
public void run() {
while (true) {
try {
Thread.sleep(1000);
for (int i = 0; i < 1000000; i++)
;
}
catch (InterruptedException ie) {
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.