Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Interface

Packages

Exception Handling

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

Multithreading

Java Multithreading
Creating a Thread
Thread Life Cycle
Thread Exceptions
Thread Synchronization

Java Applets

What is Applet?
Java Applet vs Application
Creating a Applet
Displaying Applet on Webpage
Applet Lifecycle
Applet Tag
Passing Parameters to Applet
Setting Color to applet
Update and Repaint method
Using Fonts in Applet

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Thread Exceptions


What is thread exception

  • When thread method is called which is throwing an exception the appropriate exception handler must be supplied which catch the exception thrown by thread.
  • Mostly call to sleep () method is enclosed within a try block and followed by catch block. This is because the sleep() method threads an exception, which must be caught. If exception thread is not catched, program will not compile.
  • Java run system will throw IllegalThreadStateException whenever attempt is made to invoke a method that a thread cannot handle in given state.
  • Example - a sleeping thread cannot deal with the resume () method because a sleeping thread cannot receive any instructions. Even for suspend() method when it is used on a block or NotRunnable thread.

Exceptions generated by Thread

Most commonly exceptions fetched by a thread are

  1. ThreadDeath
  2. InterruptedException
  3. IllegalArgumentException
  4. Exception

1. ThreadDeath exception example

System.out.println("Trying to kill current thread");
try 
{
    Thread.currentThread().stop();
} 
catch (final ThreadDeath ex) 
{
    System.out.println("I'm dead");
    throw ex;
}

2. Interrupted exception example

 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();
}

3. IllegalArgumentException example

void setPercentage(int pct) {
    if( pct < 0 || pct > 100) {
         throw new IllegalArgumentException("bad percent");
     }
}

4. Exception example

System.out.println("Trying to kill current thread");
try 
{
    Thread.currentThread().stop();
} 
catch (Exception ex) 
{
    System.out.println("I'm dead");
    throw ex;
}

Thread Priority


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.

What are the Thread priorities?

Thread priorities are integers that specify the relative priority. They are between 1 to 10

Priorities :

  1. MIN_PRIORITY = 1
  2. NORM_PRIORITY = 6
  3. MAX_PRIORITY = 10

Note : By default the thread have NORM_PRIORITY = 6


How to set priority to thread

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

Thread A started
A : 1
Thread C started
A : 2
A : 3
A : 4
A : 5
Thread A exited
C : 1
C : 2
C : 3
C : 4
C : 5
Thread C exited
Thread B started
B : 1
B : 2
B : 3
B : 4
B : 5
Thread B exited

How to get priority of thread?

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();

<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo