Home arrow Java & J2EE arrow More About Multithreading in Java

More About Multithreading in Java

You are reading the second part of the multithreading in Java series. The first part covered the basics of threads, explained the theory that lies behind them, and then gave examples of the two possible ways of creating new threads. This article resumes the journey by getting even deeper into concepts like the ThreadGroup class, synchronization, and inter-thread communication.

  1. More About Multithreading in Java
  2. Stopping and Synchronization
  3. Inter-Thread Synchronization and Communication
  4. Taking a Break
By: Barzan "Tony" Antal
Rating: starstarstarstarstar / 16
March 04, 2009

print this article



Before we begin, itís worth noting that this article is a sequel, so it presumes sound knowledge of the things we learned in the prior part. That part of the series was also published here, so should you have any doubts, please refer back to it. 

There are three kinds of variable types to which threads have access. The first type is local variables, but these are private, meaning that the threads cannot access each otherís variables; plus, if more threads are running the same snippet, then each thread has its own copies of the local variables. Then we have object attributes and static attributes. The latter are reachable by all threads. Object attributes can be shared (explicitly), if necessary. 

Since we can work with multiple threads, it makes sense that Java allows more than one thread to be grouped. When we are working with a huge number of threads, this actually simplifies our job, since we can manage threads by their group. Say weíre juggling five threads, and theyíre all part of a ThreadGroup. Itís so much easier to stop them by calling the method via the group, rather than doing so independently. 

Moreover, we can hierarchically add groups of threads into one thread group. This is how the entire concept turns into a tree-like hierarchy. It should be noted that threads can only access information from their own ThreadGroup, but not their parentís or otherís. We can work with Threads just like we would with arrays or sets.  

Letís see the following example:


class MyThread extends Thread{

public MyThread (String name){




public void run(){


for(int i = 0; i < 10; ++i)



catch(InterruptedException e){}




class MyThreadGroup{

public static void main (String args[]){

MyThread t[] = new MyThread[10];

for (int i = 0; i < 10; ++i)

t[i] = new MyThread("Thread no: " + i);

for (int i = 0; i < 10; i+=2)


ThreadGroup grp = Thread.currentThread().getThreadGroup();

int instanceCount = grp.activeCount();

Thread th[] = new Thread[instanceCount];

int activeCount = grp.enumerate(th);

System.out.println("Thread Group Name: " + grp.getName());

System.out.println("Count of Threads: " + instanceCount);

System.out.println("Count of Active Threads: " + activeCount);

System.out.println("List of Active Threads: ");

for (int i = 0; i < activeCount; ++i)






And guess what, the above code snippet has the following output:


Thread Group name: main

Count of Threads: 6

Count of Active Threads: 6

List of Active Threads:


Thread no: 0

Thread no: 2

Thread no: 4

Thread no: 6

Thread no: 8

 Now itís time for us to continue with some advanced ideas. 

>>> More Java & J2EE Articles          >>> More By Barzan "Tony" Antal

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: