![]() |
![]() |
Introduction of Errors | |
Java Exceptions | |
Java Try Block | |
Java Catch Block | |
Java Finally Block | |
Java Throw Keyword | |
Java Throws Keyword | |
Java Builtin Exceptions | |
User Defined Exceptions |
Java Multithreading | |
Creating a Thread | |
Thread Life Cycle | |
Thread Exceptions | |
Thread Synchronization |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Most commonly exceptions fetched by a thread are
System.out.println("Trying to kill current thread"); try { Thread.currentThread().stop(); } catch (final ThreadDeath ex) { System.out.println("I'm dead"); throw ex; }
try { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName()); Thread.sleep(1000); Thread.currentThread().interrupt(); } } catch (InterruptedException e) { e.printStackTrace(); }
void setPercentage(int pct) { if( pct < 0 || pct > 100) { throw new IllegalArgumentException("bad percent"); } }
System.out.println("Trying to kill current thread"); try { Thread.currentThread().stop(); } catch (Exception ex) { System.out.println("I'm dead"); throw ex; }
Priority decides the execution time of thread. It means at what time the thread is to be executed.
Thread is assigned a priority, which affects the order in which it is scheduled for execution or runnning.
Thread priorities are integers that specify the relative priority. They are between 1 to 10
Priorities :
Note : By default the thread have NORM_PRIORITY = 6
The thread priority can be assigned by using following method.
ThreadName.setPriority (int Value)
Example
class A extends Thread { public void run() { System.out.println("Thread A started"); for(int i=1;i<=5;i++) System.out.println("A : "+i); System.out.println("Thread A exited"); } } class B extends Thread { public void run() { System.out.println("Thread B started"); for(int i=1;i<i++) System.out.println("B : "+i); System.out.println("Thread B exited"); } } class C extends Thread { public void run() { System.out.println("Thread C started"); for(int i=1;i<=5;i++) System.out.println("C : "+i); System.out.println("Thread C exited"); } } public class Test { public static void main(String[] args) { A a1 = new A(); B b1 = new B(); C c1 = new C(); a1.setPriority(Thread.MAX_PRIORITY); b1.setPriority(Thread.MIN_PRIORITY); c1.setPriority(7); a1.start(); b1.start(); c1.start(); } }
Output
To get the current priority of thread, Following method is used.
int getPriority ()
This method will return the integer value as a priority
Example - From above example it will return the priority of Thread A as 10
a1.getPriority();