India: +91-4446 311 234 US: +1-6502 652 492 Whatsapp: +91-7530 088 009
Upto 20% Scholarship On Live Online Classes

Joining a thread

To join a thread in Java, you can use the join() method provided by the Thread class. The join() method causes the current thread to wait until the thread it is joining has finished execution.

joining thread in java

In the example above, the main thread calls myThread.join(), causing it to wait until the myThread has finished execution.

 java course for begainners will help students to understand the object-oriented programming in a better way. They will get an understanding of how to write a program using Java. The course also includes a session on how to debug programs written in Java.

Thread Priority

In Java, each thread has an associated priority that determines its scheduling priority relative to other threads. The priority of a thread is set using the setPriority method of the Thread class. The thread scheduler will allocate CPU time to the highest-priority runnable thread.

The possible priority values are integers ranging from MIN_PRIORITY (1) to MAX_PRIORITY (10). The default priority is NORM_PRIORITY (5).

thread prioriy in java

In the example above, the priority of the myThread is set to the maximum priority (10).

It is important to note that the priority of a thread does not guarantee the amount of CPU time it will receive. The actual behavior depends on the platform and the JVM implementation, as well as the relative priority of other threads.

Daemon Thread

A daemon thread in Java is a thread that runs in the background to perform tasks that are not critical to the main application. Daemon threads are usually used for services that can be safely shut down when the main application finishes.

In Java, you can make a thread a daemon thread by calling the setDaemon(true) method on the thread object before starting it.


Daemon Thread in java

In the example above, the myDaemonThread is set to be a daemon thread. When the main application finishes, the daemon thread will automatically be terminated by the JVM, even if it has not completed execution.

It is important to note that daemon threads should not be used to perform important tasks because they can be terminated at any time by the JVM without notice. Additionally, if the only threads running in a program are daemon threads, the JVM will exit, even if the daemon threads have not completed execution.

 Thread Pool & Thread Group

A thread pool in Java is a collection of worker threads that are used to execute tasks. Instead of creating a new thread for each task, the task is added to a queue, and a worker thread from the pool picks up and executes the task. This can improve performance by reducing the overhead of creating and destroying threads for each task, and it can also be used to limit the number of concurrent threads.

The java.util.concurrent.Executor interface is the main interface for thread pools in Java. One of its implementations is the java.util.concurrent.Executors class, which provides methods for creating thread pools.

A thread group in Java is a mechanism for grouping threads together. A thread can belong to only one thread group. The ThreadGroup class provides methods for creating and managing thread groups, and for getting information about the threads in the group.


Thread Pool & Thread Group in java

In the example above, a thread pool with a fixed size of 5 worker threads is created using Executors.newFixedThreadPool(5). The task MyTask is submitted 10 times to the thread pool, and the worker threads in the pool will pick up and execute the tasks.

Thread groups can be used to manage and monitor the state of multiple threads. For example, you can stop all threads in a thread group, or you can set the priority of all threads in a group.