A thread can be described as the light-weight smallest part of a process that can run simultaneously with the remaining parts (other threads) of the same process. Threads are not dependent on each other because they all have different path of execution, hence if an exception takes place in one thread, it doesn’t affect the working of other threads. All threads of a process share the common memory. When multiple threads are executed concurrently it is called as multithreading.
The main reason of using multithreading is to provide concurrent execution of two or more parts of a program to maximize the CPU utilization time. A multithreaded program contains two or more parts that can run simultaneously, such part of a program is known as a thread. Multithreading is used to utilize the maximum CPU time so that the idle time can be kept to minimum.
The two categories of threads are user thread and daemon thread.
Lifecycle of a thread is as shown below:
Every Java thread has a priority, which aids the operating system in determining the thread scheduling order.
The range of Java thread priorities is between MIN PRIORITY (a constant of 1) and MAX PRIORITY (a constant of 10). NORM PRIORITY is the default priority for all threads (a constant of 5).
Higher-priority threads are more vital to a program and should be given priority over lower-priority threads. On the other hand, Thread priorities cannot ensure the order in which threads execute and are very platform dependant.
Main.java:10: error: cannot find symbol
multiThread1.setName("First Thread");
^
symbol: method setName(String)
location: variable multiThread1 of type Main
Main.java:11: error: cannot find symbol
multiThread1.setPriority(Thread.MIN_PRIORITY);
^
symbol: method setPriority(int)
location: variable multiThread1 of type Main
Main.java:14: error: cannot find symbol
multiThread2.setName("Second Thread");
^
symbol: method setName(String)
location: variable multiThread2 of type Main
Main.java:15: error: cannot find symbolz
multiThread2.setPriority(Thread.MAX_PRIORITY);
^
symbol: method setPriority(int)
location: variable multiThread2 of type Main
Main.java:18: error: cannot find symbol
multiThread3.setName("Third Thread");
^
symbol: method setName(String)
location: variable multiThread3 of type Main
Main.java:20: error: cannot find symbol
multiThread1.start();
^
symbol: method start()
location: variable multiThread1 of type Main
Main.java:21: error: cannot find symbol
multiThread2.start();
^
symbol: method start()
location: variable multiThread2 of type Main
Main.java:22: error: cannot find symbol
multiThread3.start();
^
symbol: method start()
location: variable multiThread3 of type Main
|