Register   Login   About   Study   Enterprise   Share
Internet / AI Technology University (ITU/AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 3. Threads and Network >> 3.1. Threads
Current Topic: 3.1.3. Using ThreadGroup
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.
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<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?



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.

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy