Multithreading example in Java

In the previous post I discussed about multithreading concepts and provided a case study to understand the principles better. In this post the solution of the case study is given with detailed explanation about it.

As mentioned, the first thing that program should do is to categorize mails, this is rather simple and no need to involve multithreading and it can be resolved with a simple if, else condition.

Check the below sample code for better understanding,

Vector mailQueue = new Vector(); // The main unorganized mail queue
mailQueue = getAllMails();
Vector northMails = new Vector();
Vector southMails = new Vector();
Vector eastMails = new Vector();
Vector westMails = new Vector();
// Start categorizing the mails
for(int cnt=0;cnt
{
    Mails oneMail = mailQueue.get(cnt);
    if(oneMail.getMailAdress().toUpperCase.contains(“NORTH”))
    {
        northMails.add(oneMail);
    }
    else if(oneMail.getMailAdress().toUpperCase.contains(“SOUTH”))
    {
        southMails.add(oneMail);
    }
    else if(oneMail.getMailAdress().toUpperCase.contains(“EAST”))
    {
        eastMails.add(oneMail);
    }
    else
    {
        westMails.add(oneMail);
    }
}
Vector<vector> allMails = new Vector();
allMails.add(northMails);
allMails.add(southMails);
allMails.add(eastMails);
allMails.add(southMails);

We have categorized the main queue to 4 groups (vectors). Now it is time to send each mail man (vector) to operate separately (run in a separate thread) the same task which is mails distribution. However, we should bear in mind about the last portion of the case study which refers to security man waiting, therefore, we need to keep track of the threads and to this so need to create an array to holds thread reference. Check the below code,

//Creating ArrayList of the ThreadClass to hold reference of the threads
ArrayList<ThreadClass> mailManThreads= new ArrayList(); 
for(int cnt=0;cnt
{                     
Vector oneMailQueue = new Vector();
oneMailQueue = allMails.get(cnt);
ThreadClass oneMailManThread = new ThreadClass (); 
oneMailManThread .start(oneMailQueue);                     
mailManThreads.add(oneDealerThread);
}
//Joins all threads to main thread to avoid main thread to continue without finishing the rest of threads
for(int cnt=0;cnt<mailManThreads.size();cnt++)                 
{                     
mailManThreads.get(cnt).join(); 
}

The only remaining and most challenging part is to avoid mailmans (threads) to access to the notebook (resource) at the same time. If one resource will be access at the same time, race condition might occur and the value of one resource might be changed without notifying others resource holders. To avoid such a problem the best approach is using locking mechanism in which resource cannot be allocated to more than one thread at the sametime. This is done with using synchronized keyword. The entire operation of mail main is written in a separate class file which is as well.

public class ThreadClass extends Thread 
{
private Thread thread;
private Vector<Mails> mailQueue = new Vector<Mails>();
public void start(Vector<Mails> mailQueue)     
{         
if(thread == null)         
{             
this.mailQueue  = mailQueue;             
thread = new Thread(this);             
thread.start();         
}     
}
public void run()     
{
synchronized(this)
{
            updateNoteBook(); //Update share resource
}
// Other operation here
}
public static synchronized updateNoteBook()
{
    // Do whatever necessary
    // The function can be called by one thread at the same time
}
}

As you can see multithreading is not as cumbersome as it seems, just you need to learn the basic concept first and find an example (preferably real world) to implement to become master of it. 😉

Send your idea and information to kasra@madadipouya.com

Leave a Reply