Sitemap
Hansinigunasena

Hansinigunasena

Threads in Java

4 min readAug 18, 2022

Thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

If we need 10 min to complete a task using one thread, it does not mean that we can complete the task with 10 threads with 1 minute.

There are many cases where dependency is applied to threads. That means, one thread need some other thread to execute before it start to execute. (Example : validate date before saving) But we can cut down the time little bit and execute threads simultaneously.

Multithreading :

It meaning is Multiple processes. Multi tasking.

There are two ways of multitasking.

1. Process Based — Doing multiple things in same time. (Watching movie while downloading video and transfroming file to another drive at same time) if any of these task is stopped, it does not affect others. So processeor can run multiple task at same time.They are multiple programs. This is run on operating system.

2. Thread Based — These are belongs to same process which is running a computer. If we have 50 files to process, if one file take 1 second to finish, by using 2 threads we can speed up the processing. But the maximum amount of threads we can use is 50, and we can not use 100 for speed up. Thread is a flow of execution. It execute instructions. In certain point it may create multiple threads and do the processing and one point it may again combine and do as a single thread.

There are 2 ways to creating threads.

1. Extends thread class

2. Implements Runnable interface

Java does not support multiple inheritence, at the time of extending thread class to class, it breaks any other extends. So the objects of that class become a thread. Java support multiple implementations.

Java terminated program in 2 cases.

1. If call system.exist()

2. After executing the last non deamon thread.

Thread implementations.

  1. It is not a must to override the run method in a thread class, but if we don’t override it it does not do anything. (for this case it should Extend from Thread class)
  2. If we impplements runnable inteface we need to implement run method.
  3. When we start a thread, it is adding to a thread schedular. NO guarantee on executing order.
  4. If we directlt call the run method, other than calling start method, it execute as a normal class. It has only one thread. It is the main method.

5. always thread class run method will execute with no arguments.

6.Thread runs until last non deamon thread executes. if child thread is set to deamon true, Main thread will finish and it stop executing the program.

Implementing Runnable interface

Get Hansinigunasena’s stories in your inbox

Join Medium for free to get updates from this writer.

We need to create Instance of thread class and pass runnable instance to that when dealing with Runnable interface. (Thread class have multiple constructors, we are using runnable instance )

Other ways :

  1. passing thread name
  2. pass runnable object
  3. Passing thread group
Press enter or click to view image in full size

Thread Priority

Thread priority is a value. It starts with 1 and ends with 10.

Highest priority is 10, lowest is 1, medium is 5.

In any application main thread priority is 5. (default priority , if undefined the priority). Child thread inherits main thread’s priority.

If we set priority higher than 10, it produce an illegal argument exception.

Press enter or click to view image in full size

By using thread dumps we can get the status of particular threads.

Thread life cycle

Thread.join()

If one thread is need to have a dependency with another thread, if the first thread is t1 and second thread is t2 , t1 should call join method.

so t1 will call = t2.join()

t1 call join on t2.

we can apply some conditions there. Giving time to complete. if it is not define it is forever. if t2 complete its work before given time then it go to next step.

t2.join(600);//applying conditions

After calling t2.join, t1 becomes waiting state. t2 is busy. Again if t1 need to execute it need to go for ready or runnable state. t1 will go to any other state from waiting under following conditions.

  1. if t2 finish work
  2. if some one interrrupts
  3. if time out

yield()

Need for debugging scenario.

It(t1) tells thread schedular i am okay give chance to other thread. If thread schedular give chance to another thread(t2 or t3), first thread go to wait. if t2 get the chance after executing thre is no gurantee t1 will start next. may be t3 will start. otherwise t1 is in waiting.

Thread.sleep()

it should have a parameter. when sleep call : thread will go to waiting state. It comes back if waiting time expires, or interrupt.

Interrupt() method use to interrupt. One interrupt only work for one time.

when call interrupt on non sleeping thread it execute normally, it waits until thread go to waiting state, at the movement the thread is go to sleep or wait it interrupt.

--

--