Java - Provide a very short quick comment on each area with /*comment*/ listed e
ID: 3880162 • Letter: J
Question
Java - Provide a very short quick comment on each area with /*comment*/ listed explaining what the code is doing
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
public class CreateThreadGroups
{
public CreateThreadGroups() {
/* Creating three ThreadGroups “alpha”,”beta”,”theta” to group threads.*/
ThreadGroup alpha = new ThreadGroup("alpha");
ThreadGroup beta = new ThreadGroup("beta");
ThreadGroup theta = new ThreadGroup(alpha, "theta");
/* Creating threads in each threadgroup. First argument of thread class constructor is the threadgroup name. * Second argument is the Runnable implemented class. Hence, creating anonymous threads and starting it */
(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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.