Unit 3
Unit 3
interface InterfaceName
{
variables declaration;
methods declaration;
}
• The code for the method is not included in the interface and the
method declaration simply ends with a semicolon.
• The class that implements this interface must define the code
for the method.
Ex:2 interface Area
{
final static float pi 3.142F;
float compute (float x, float y);
void show ();
}
Difference between class & Interface
Class Interface
1. The members of an interface are
1. The members of a class can
always declared as constant,i.e.,
be constant or variables. their values are final.
catch (InterruptedException e)
{ …………………
............// Cannot handle it in the current state
}
catch (Illegal ArgumentException e)
{ ……………………..
…………………… // Illegal method argument
}
catch (Exception e)
{ ……………….
…………….. // Any other
}
Thread Priority
• In Java, each thread is assigned a priority,
which affects the order in which it is
scheduled for running.
• The threads of the same priority are given
equal treatment by the Java scheduler and,
therefore, they share the processor on a first-
come, first-serve basis.
• Java permits to set the priority of a thread
using the setPriority() method as follows:
ThreadName.setPriority (intNumber);
• The intNumber is an integer value to which
the thread's priority is set.
• The Thread class defines several priority
constants:
• MIN_PRIORITY = 1
• NORM_PRIORITY = 5
• MAX_PRIORITY = 10
• Note:
We can get and set the priority values for
threads in Java using the getPriority() and
setPriority() methods.
Based on these priorities, the thread
scheduler decides the order of thread
execution.
We should also remember that there is no
guarantee that the scheduler always executes
the high priority threads first since it also
depends on the JVM implementation.
• Most user-level processes should use NORM_PRIORITY,
plus or minus 1
• By assigning priorities to threads, it ensure that they are
given the attention they deserve.
• For example, we may need to answer an input as
quickly as possible.
• Whenever multiple threads are ready for execution, the
Java system chooses the highest priority thread and
executes it.
• For a thread of lower priority to gain control, one of the
following things should happen:
1. It stops running at the end of run().
2. It is made to sleep using sleep().
3. It is told to wait using wait().
• However, if another thread of a higher priority
comes along, the currently running thread will
be preempted by the incoming thread thus
forcing the current thread to move to the
runnable state.
• Remember that the highest priority thread
always preempts any lower priority threads.
Synchronization
• Threads that use their own data and methods
provided inside their run() methods.
• What happens when they try to use data and
methods outside themselves?
• On such occasions, they may compete for the same
resources and may lead to serious problems.
• For example, one thread may try to read a record
from a file while another is still writing to the same
file.
• Depending on the situation, we may get strange
results
• Java enables us to overcome this problem
using a technique known as synchronization.
• In case of Java, the keyword synchronised
helps to solve such problems by keeping a
watch on such locations.
• For example, the method that will read
information from a file and the method that
will update the same file may be declared as
synchronized.
Ex: synchronized void update()
{…………………
………………… // code here is
}
Thread A Thread B
synchronized method2( ) synchronized method1( )
{ {
synchronized method1() synchronized method2()
{……………… {………………
……………….. ………………..
} }
} }
Implementing The 'Runnable' Interface
• How to make use of the Runnable interface to implement
threads.
• The Runnable interface declares the run() method that is
required for implementing threads in our programs.
• Steps
1. Declare the class as implementing the Runnable
interface.
2. Implement the run() method.
3. Create a thread by defining an object that is
instantiated from this "runnable" class as the target
of the thread.
4. Call the thread's start() method to run the
thread.
Java Program using Runnable Interface
class X implements Runnable
{
public void run()
{
for (int i = 1; i<=10; i++)
{
System.out.println("\tThreadX : " +i);
try{Thread.sleep(1000);}catch(Exception e){}
}
System.out.println("End of ThreadX");
}
}
class RunnableTest
{
public static void main(String args[])
{
X runnableobj = new X();
Thread threadobj = new Thread(runnableobj);
threadobj.start();
System.out.println("End of main Thread");
}