Java
  Home arrow Java arrow More About Multithreading in Java
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

More About Multithreading in Java
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2009-03-04


    Table of Contents:
  • More About Multithreading in Java
  • Stopping and Synchronization
  • Inter-Thread Synchronization and Communication
  • Taking a Break

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    More About Multithreading in Java
    ( Page 1 of 4 )

    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.

    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){

    super(name);

    }

     

    public void run(){

    try{

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

    sleep(1000);

    }

    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)

    t[i].start();

    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)

    System.out.println(th[i].getName());

    grp.interrupt();

    }

    }

     

    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:

    main

    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 Articles          >>> More By Barzan "Tony" Antal
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT