You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
A classic example of ThreadGroup usage is a browser security check preventing applets from setting their priority higher than system threads.
The method checkAccess() of the ThreadGroup class can be used to check if the current thread has the rights to modify the ThreadGroup object.
Another example could be an application where we need to provide multiple services each requested by multiple clients.
In such a case we can create the ThreadGroup for each service and create Thread for each client request on that service.
Then we can control the group of threads by setting a priority for the group, monitoring a number of active threads inside a group, and so on...
package its.day14.threads;
public class MyThreadGroup {
public MyThreadGroup() {
// create a group of threads
ThreadGroup netAndDbServices = new ThreadGroup("netAndDb"); // parent
ThreadGroup dbServices = new ThreadGroup(netAndDbServices, "db");
ThreadGroup netServices = new ThreadGroup(netAndDbServices, "net");
// use MyThread class that extends the Thread to create a thread to service next client
MyThread client1Service = new MyThread(dbServices,"service1");
MyThread client2Service = new MyThread(dbServices,"service2");
// start one thread - activate
client1Service.start();
// count all active threads in the group
int numberOfActiveThreads = dbServices.activeCount(); // 1
Thread[] list = new Thread[numberOfActiveThreads];
int active = dbServices.enumerate(list); // fill the list with active threads
for(int i = 0; i < active; i++) {
// display active threads names
System.out.println(i + ": " + list[i] );
}
}
// test the class
public static void main(String[] args) {
MyThreadGroup group = new MyThreadGroup();
}
}
Was it clear so far?
Assignments:
1. Open Eclipse and add (type) the MyThreadGroup class to the package its.day14.threads in the project week6.threads.
2. Get rid of errors and Run – As Java Application.
<br/>package its.day14.threads;
<br/>
<br/>public class MyThreadGroup {
<br/>
<br/> public MyThreadGroup() {
<br/> // create a group of threads
<br/>
<br/> ThreadGroup netAndDbServices = new ThreadGroup("netAndDb"); // parent
<br/> ThreadGroup dbServices = new ThreadGroup(netAndDbServices, "db");
<br/> ThreadGroup netServices = new ThreadGroup(netAndDbServices, "net");
<br/>
<br/> // use MyThread class that extends the Thread to create a thread to service next client
<br/>
<br/> MyThread client1Service = new MyThread(dbServices,"service1");
<br/> MyThread client2Service = new MyThread(dbServices,"service2");
<br/> // start one thread - activate
<br/> client1Service.start();
<br/> // count all active threads in the group
<br/> int numberOfActiveThreads = dbServices.activeCount(); // 1
<br/>
<br/> Thread[] list = new Thread[numberOfActiveThreads];
<br/>
<br/> int active = dbServices.enumerate(list); // fill the list with active threads
<br/>
<br/> for(int i = 0; i < active; i++) {
<br/> // display active threads names
<br/> System.out.println(i + ": " + list[i] );
<br/> }
<br/> }
<br/> // test the class
<br/> public static void main(String[] args) {
<br/> MyThreadGroup group = new MyThreadGroup();
<br/> }
<br/>
<br/>}
<br/>
Was it clear so far?
onclick="window.location.href='/BASE/jsp/demo.jsp?checkFlavor=itsp&issueID=65&intro=general&group=aitu&ur=f'">
Assignments:
1. Open Eclipse and add (type) the MyThreadGroup class to the package its.day14.threads in the project week6.threads.
2. Get rid of errors and Run – As Java Application.