Java Unit 3
Java Unit 3
Two Marks
1)What is a package?(apr-13,nov-14,nov-15,apr-16)
Packages contain a set of classes in order to ensure that class names are unique. Packages
are containers for classes that are used to compartmentalize the class name space. Packages are
stored in a hierarchical manner and are explicitly imported into new class definition. synatx:
package packagename;
2)What is an interface?(apr-12,nov-13,apr-14,apr-15,apr-16)
An interface is a collection of abstract behavior that individual classes can implement. An
interface is created in the same way as a class. Java does not support multiple inheritance to
overcome it interface has been used. All the methods of an interface are automatically public.
Syntax : Interface name
{
variable declarations;
return type method name1 (parameter list)
}
4)What is synchronization?(apr-12,nov-13,nov-14)
Two or more threads accessing the same data simultaneously may lead to loss of data
integrity. java enables to overcome this problem using technique known as synchronization.The
key word synchronized is used,so that the method that will read information from a file and the
method that will update the same file may be declared as synchronized.
syntax: synchronized void update()
{
statements;
}
5)What is a Thread?(apr-14,apr-15,nov15)
A thread is a line of execution. It is the smallest unit of code that is dispatched by the
scheduler. Thus, a process can contain multiple threads to execute its different sections. This is
called multithread.
Extending interface :An interface can be interfaced from other interface using the keyword
called extends. Syntax:
Implementing an interface : A class can extend only a single class, it can implement a number
of interface. The keyword implements is used to implement an interface. Syntax:
class class name extends super class implements interface
{
Body of the statement;
}
Example:
interface area
{
final static float pi = 3.14f ;
float compute ( float x , float y ) ;
}
class Rectangle implements Area
{
public float compute ( float x , float y ) ;
{
return ( x * y) ;
}
}
class Circle implements Area
{
public float compute ( float x , float y)
{
return ( pi*x*y) ;
}
}
class InterfaceTest
{
public static void main(String args [ ]) throws IOException
{
Rectangle rect = new Rectangle ( ) ;
Circle cir = new Circle ( ) ;
Area a ;
a = rect ;
System.out.println ( “ Area of Rectangle”+a.compute(10,30)) ;
a = cir ;
System.out.println( “ Area of Circle”+a.compute(20,0));
}
}
Output :
Start()
Suspend() Resume()
Sleep() Notify()
Wait () Stop()
2)Runnable state :
The thread is in runnable state after invocation of start() method, but the thread
scheduler has not selected it to be the running thread. The process of assigning time to threads is
known as time-slicing.
3)Running state :
The thread comes to this state when it start its execution. A thread enters this stage
whenever its run ( ) method is called. It has been suspended using suspend ( ) method. A suspend
thread can be reviewed by using the resume ( ) method.sleep() method time will be in
millisecond.
4)Blocked state :
A thread comes to this state when it is made to stop its execution. The thread comes to
this state when its stop ( ) or wait ( ) method is called.
5)Dead state :
A thread comes to this state when it completes its execution.
1. start() method
2. run() method
3. sleep() method
4. stop() method
5. wait() method
6. yield() method
1.start() method: This method is used to start a new thread. When this method is called the
thread enters the ready to run mode and this automatically invokes the run( ) method.
Syntax:void start( ).
2.run() method: This method is the important method in the thread and it contains the
statements that are to be executed in our program. It should be overridden in our class, which is
derived from Thread class.Syntax;
void run( )
{
//Statements implementing thread
}
3.sleep() method: This method is used to block the currently executing thread for the specific
time. Syntax: void sleep(time in milliseconds )
4.stop() method: It is used to stop the running thread. Syntax: void stop().
5. wait() method: This method is used to stop the currently executing thread until some event
occurs. Syntax: void wait()
6. yield() method: This method is used to bring the blocked thread to ready to run mode.
Syntax: void yield()
Whenever multiple threads are ready for execution,java system chooses the highest
priority thread and executes. If lower priority to gain control one of the 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().
Example:
}
public static void main(String args[]){
TestMultiPriority1 m1=new TestMultiPriority1();
TestMultiPriority1 m2=new TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
}
}
Output: running thread name is:Thread-0
running thread priority is:10
running thread name is:Thread-1
running thread priority is:1
Start()
Suspend() Resume()
Sleep() Notify()
Wait () Stop()
New born
Start Stop
Runnable Dead state
method
2)Runnable state :
The thread is ready for execution and waits for the processor to be available. All
the threads in a execution queue have equal priority. The threads comes to this state when the
start ( ) method is called. This process of assigning time to threads is known as time-slicing.If we
want a thread to relinguish control to another thread equal priority before its turn comes we can
do so by using the yield ( ) method.
…… …….
.. …
Running thread … Runnable thread
.
…
h
3)Running state :
. comes
The thread to this state when it start its execution. A thread enters this stage
whenever its run ( ) method is called. It has been suspended using suspend ( ) method. A suspend
thread can be reviewed by using the resume ( ) method.
Suspend
Resume
We can put a thread in a specified time period using the method sleep ( ) method where time is in
millisecond.
Sleep (t)
After (t)
It has been told to wait until some event occurs. This is done using the wait ( ) method. The
thread can be scheduled to run again using the notify ( ) method.
Wait
Notify
4)Blocked state :
A thread comes to this state when it is made to stop its execution. The thread comes to
this state when its stop ( ) or wait ( ) method is called.
5)Dead state :
A thread comes to this state when it completes its execution.
Syntax :
Interface name
{
data type varname 1 = value 1;
...................................................;
...................................................;
data type varname n = value n;
return type method name 1 (parameter list);
.......................................................................;
.......................................................................;
return type method name n (parameter list);
}
Example :
Interface x
{
int a=7 , b=8 , n=9 ;
void a (int , int);
void a (int);
Extending interface :An interface can be interfaced from other interface using the keyword
called extends.
Syntax: interface sub class extends base class
{
Variable declarations ;
Method declaration ;
}
Implementing an interface :A class can extend only a single class, it can implement a number
of interface. The keyword implements is used to implement an interface.
Syntax: class class name extends super class implements interface
{
Body of the statement;
}
Output :
Area of Rectangle 300.0
Area of Circle 1256.0
Example:
class MyThread implements Runnable
{
public void run()
{
System.out.println("concurrent thread started running..");
}
}
class MyThreadDemo
{
public static void main( String args[] )
{
MyThread mt = new MyThread();
Thread t = new Thread(mt);
t.start();
}
}
Output :
concurrent thread started running..
classMyThreadDemo
{
public static void main( String args[] )
{
MyThread mt = new MyThread();
mt.start();
}
}
Output :
concurrent thread started running..