Home College Assignments Thread Programming In java

Thread Programming In java

Thread Programming In java

Thread programming in java is concerned with how system utilizes its resources. Friendly interface, computations and other intensive tasks can take up a lot of processor time. A java program that is doing something that consumes a lot of system time, might affect the program’s interface view .i.e. dialog boxes respond slowly, drop down list takes few seconds to appear or even buttons click are recognized slowly etc.

How to resolve this issue? Well one solution is to divide the program into chunks of processes that can run simultaneously; however, this technique is not preferred as processes use more space. Processes need their own resources and identity to work. They cannot share their belongings with others. We need something that is resource efficient and works simultaneously. Thread programming in Java will help us.

So what are threads? They are parts of a program that perform something specific independently while the program does something else. This concept is also known as multitasking. We divide our program into chunks that can work independently. When their assigned task is done, they report the main program. Hold on a second, our aim is to minimize resource utilization, are threads resource efficient?

Yes, they share resources with each other. That’s a big advantage over processes.

Let’s learn how they work….

class ThreadStuff implements Runnable { // using Runnable interface

Thread t; // Thread class instance
String ThreadName; // String that holds Thread Names
	ThreadStuff(String name) // constructor with thread name as argument
		ThreadName = name;
		System.out.println("Created Thread Name : "+ ThreadName);
	public void start () // method checks whether thread is initialized or not
		System.out.println("Starting "+ ThreadName);
		if (t ==null) // if not initialized, do it
			t= new Thread (this,ThreadName);
			t.start(); // after that, start that thread
	public void run () // contains all the tasks that a thread will do
	System.out.println("Running Thread : "+ ThreadName);
	try{	// thread might get interrupted
		for (int counter = 0; counter<=4;counter++)
			System.out.println("Thread:"+ ThreadName + "," + counter);
	catch (InterruptedException e ) // catch block in case of interruption
		System.out.println("Thread : "+ ThreadName + "Interrupted");
	System.out.println("Thread : "+ ThreadName + " Exiting"); // threads are terminating
} // end ThreadStuff class

class Threads  // class that will govern threads
	public static void main(String[] args) {

		ThreadStuff T1 = new ThreadStuff ("THREAD_1");
		ThreadStuff T2 = new ThreadStuff ("THREAD_2"); // initializing 3 threads with their names
		ThreadStuff T3 = new ThreadStuff ("THREAD_3");
		T2.start(); // starting threads
	} // end main method

} // end sample class

This program contains two classes, one that holds all the things regarding threads called ‘ThreadStuff’ and the other is the governing class that manipulates threads.

ThreadStuff has two methods, run and start. run() method contains all the functionality that is assigned to the thread. Start() method assures that threads are initialized and it calls another method start() which in turn calls the run() method. Confused with the concept? Last line of start method invokes another method to start working, and as we know all the working is placed under run () method. So we end up with this concept that after initialization of a thread, it call start () method that calls run () method to perform thread tasks.

Another interesting thing is that program has no definite output, if you focus. All the threads run randomly, although we coded them in a sequence. Why is that so?

Again the same answer, threads works independently. They are in waiting state of a queue that processor handles. Each time you run the program, all the threads get different locations in the queue. Your never know who is at the top. Now you know the magic trick, share something you experienced.