Threads in Java

Multithreading application are really powerful applications. These applications can solve multiple tasks in the same time. Java programming language offers a good and flexible mechanism for using threads in your programs. There are two possibilities to create your own threads: the first way is to implement interface Runnable and the second way is to extend class Thread. I'll show you the both variants. The interface Runnable consists of one method:
  1. public interface Runnable {    
  2.         public abstract void run();
  3. }
So, to create a thread class you need to override the run() method. I'll show you an example of a thread, that will show messages with a specified interval of time:
  1. public class RunnableThread implements Runnable
The members of this class are:
  1. Thread myThread;
  2. int sleep_time;
  3. String Tname;
We need to have a thread object in the class to start your own thread. The constructor of the class is presented here:
  1. RunnableThread(String name, int _sleep_time){
  2.                 sleep_time = _sleep_time;
  3.                 Tname= name;
  4.                 myThread = new Thread(this,name);
  5.                 System.out.println("New thread created "+myThread);
  6.                 myThread.start();
  7.         }
In the constructor start() method is called. This method calls the method run() that must be overridden:
  1. @Override
  2.         public void run() {
  3.                 // TODO Auto-generated method stub
  4.                 try{
  5.                         for (int i = 10; i > 0; --i){
  6.                                 System.out.println(myThread+" "+i);
  7.                                 myThread.sleep(sleep_time);
  8.                         }
  9.                 }
  10.                 catch(InterruptedException e){
  11.                         System.out.println(myThread+" interrupted");
  12.                 }
  13.                 System.out.println(myThread+" terminated");
  14.         }
Also, in this class there is a method, used to set the priority of the thread:
  1. public void setPriority(int p){
  2.                 myThread.setPriority(p);
  3.         }      
The second way of extending the Thread class consist of defining additional properties to the base class:
  1. public class ThreadExt extends Thread
Now, you can add any feature you want. I did it in the simple way by adding a message to be shown in the constructor after calling the constructor of the super class:
  1. ThreadExt(String _name){
  2.         super(_name);
  3.         System.out.println(_name+" "+this);
  4.         start();       
  5. }
The method run is defined similarly to the way I did it in the first case:
  1. public void run(){
  2.                 try{
  3.                         for (int i = 10; i > 0; --i){
  4.                                 System.out.println(this+" "+i);
  5.                                 Thread.sleep(300);
  6.                         }
  7.                 }
  8.                 catch(InterruptedException e){
  9.                         System.out.println(this+" interrupted");
  10.                 }
  11.                 System.out.println(this+" terminated");
  12.         }
It shows a message with the interval of 300 milliseconds. Now we can test both methods. Let's create three thread object in the public static void main() method:
  1. RunnableThread t1 = new RunnableThread("First",500);
  2. RunnableThread t2 = new RunnableThread("Second",100);
  3. ThreadExt t3 = new ThreadExt("Third");
Now we can set different priorities to the objects:
  1. t1.setPriority(1);
  2. t1.setPriority(10);
  3. t3.setPriority(4);
I wan't to show the priorities of all threads only after all the threads are terminated.That's why I'm using join() method to wait for thread termination in main thread:
  1. System.out.println("Waiting for threads termination");
  2.                 try{
  3.                         t1.myThread.join();
  4.                         t2.myThread.join();
  5.                         t3.join();
  6.                 }
  7.                 catch(InterruptedException e){
  8.                         System.out.println("Main thread terminated");
  9.                 }
Join method can throw InterruptedException if the main thread is interrupted. So a try/catch block is necessary here. Now I can show the priority of all threads:
  1. System.out.println("Priority of the first thread was "+t1.myThread.getPriority());
  2.                 System.out.println("Priority of the second thread was "+t2.myThread.getPriority());
  3.                 System.out.println("Priority of the third thread was "+t3.getPriority());
The output of this program is:
  1. New thread created Thread[First,5,main]
  2. New thread created Thread[Second,5,main]
  3. Thread[First,5,main] 10
  4. Thread[Second,5,main] 10
  5. Third Thread[Third,5,main]
  6. Waiting for threads termination
  7. Thread[Third,4,main] 10
  8. Thread[Second,5,main] 9
  9. Thread[Second,5,main] 8
  10. Thread[Second,5,main] 7
  11. Thread[Third,4,main] 9
  12. Thread[Second,5,main] 6
  13. Thread[First,10,main] 9
  14. Thread[Second,5,main] 5
  15. Thread[Second,5,main] 4
  16. Thread[Third,4,main] 8
  17. Thread[Second,5,main] 3
  18. Thread[Second,5,main] 2
  19. Thread[Second,5,main] 1
  20. Thread[Third,4,main] 7
  21. Thread[First,10,main] 8
  22. Thread[Second,5,main] terminated
  23. Thread[Third,4,main] 6
  24. Thread[First,10,main] 7
  25. Thread[Third,4,main] 5
  26. Thread[First,10,main] 6
  27. Thread[Third,4,main] 4
  28. Thread[Third,4,main] 3
  29. Thread[First,10,main] 5
  30. Thread[Third,4,main] 2
  31. Thread[First,10,main] 4
  32. Thread[Third,4,main] 1
  33. Thread[Third,4,main] terminated
  34. Thread[First,10,main] 3
  35. Thread[First,10,main] 2
  36. Thread[First,10,main] 1
  37. Thread[First,10,main] terminated
  38. Priority of the first thread was 10
  39. Priority of the second thread was 5
  40. Priority of the third thread was 4
The output of this program will be different on different pc. This is a simple tutorial about threads, but I hope, it can help you to understand more difficult thing. The whole eclipse project is attached, so you can review it.
Tags

Add new comment