0% found this document useful (0 votes)
3 views29 pages

Module 4 Final

Uploaded by

Sanjay Manjunath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views29 pages

Module 4 Final

Uploaded by

Sanjay Manjunath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Packages & Multithreading

PACKAGES
Package
Definition: A java package is defined as a group of similar types of classes, interfaces
and sub-packages.
Packages in java is a mechanism to encapsulate a group of classes, interfaces and sub
packages.
Advantages of Java Package
1. Java package is used to categorize the classes and interfaces so that they can be
easily located and maintained.
2. Java package provides access protection.
3. Java package removes naming collision.
4. Reusability: Reusability of code is one of the most important requirements in
the software industry. Reusability saves time, effort and also ensures
consistency. A class once developed can be reused by any number of programs
wishing to incorporate the class in that particular program.
Types of packages
Packages in java can be categorized in two forms:
1. Built-in packages
2. User-Defined packages.
1. Built-in packages: These are predefined packages in java library. There are many
built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Following
figure shows some built-in packages.

Page 1
Packages & Multithreading

2. User-Defined Packages: Programmers can define their own packages to bundle


group of classes/interfaces, etc. Such packages are called user-defined packages.
Creating and Defining a Package
The package statement should be the first line in the source file. There can be only one
package statement in each source file at the beginning of the program to include that
program in the specified package.
Syntax:
package package_name;
Ex:
package college;
Example:
//filename: [Link]
package college; //package statement should be at the beginning of the program.
public class Library {
public String id;
public void setId (String id)
{
[Link]=id;
}
public String getId()
{
return id;
}
}
In the above example, the class Library is included in the package college.

How to compile java user-defined package


If you are not using any IDE, you need to follow the syntax given below at the command
prompt:
Syntax:
javac -d directory javafilename
Ex:
javac –d . [Link]
Here, the -d option specifies the destination where to put the generated class file.
The . (dot) denotes current working directory (CWD).
The above command creates a .class file in the specified package.

Page 2
Packages & Multithreading

How to use user-defined package


The import Keyword
The package can be imported using the import keyword and the fully qualified name of
the class can be used.

Syntax:
import [Link];
Ex:
import college. Library;

Program: Write a java program to create user-defined package.

//filename: [Link]
package pack1; //creating pack1 package

public class GreetingCode


{
public static void greet()
{
[Link]("Hello guys....... welcome");
}
}

Compile:
javac –d . [Link]
It creates [Link] file in the package(directory) pack1 in the current
directory.
Suppose if we want to add one more class ThankingCode to the same package pack1.

//filename: [Link]
package pack1; //adding one more class to pack1 package
public class ThankingCode
{
public static void thank()
{
[Link]("Bye guys..... All The Best.... ");
}
}

Compile:
javac –d . [Link]
It creates [Link] file in the package (directory) pack1 in the current
directory.

Page 3
Packages & Multithreading

Now import the above package pack1 to another program [Link] and call
greet() and thank() .

//filename: [Link]

import pack1.*; // importing pack1 package

class PartyCode
{
public static void main(String args[])
{
[Link]();
[Link](“A great evening to all of you, enjoy the party.......”);
[Link]();
}
}

Compile:
D:\javaprograms> javac [Link]
D:\javaprograms> java PartyCode

Output:
Hello guys....... welcome
A great evening to all of you, enjoy the party........
Bye guys..... All The Best....

Note: 1 It is easy to organize class files into packages. All we need to do is put related
class files in the same directory, give the directory a name that relates to the
purpose of the classes, and add a line to the top of each class file that declares
the package name, which is the same as the directory name where they reside.

Note:2 To import all items in a package, no need to refer them individually but we can
import them collectively by using the asterisk * is a wild character of Windows
which means all the files (means all the classes).

Ex: import [Link].*;


import college.*;

Note:3 If you import a package, subpackages will not be imported.

Page 4
Packages & Multithreading

Nesting Packages

Package inside the package is called the subpackage. This process is called nesting
packages.

It means we can also create any no. of packages inside another package.

To nest one package inside another package, use the following syntax:

package package1.package2.package3.................... ;

Here package2 is a subpackage of package1 and package3 is a subpackage of package2.

Ex: package [Link];

Here library is a subpackage of college package.

Program: Write a java program to illustrate nesting packages.

//filename: [Link]
package nestedpackage; //creating nestedpackage
public class PackCode1
{
int a;
int b;
public PackCode1(int x, int y)
{
a=x;
b=y;
}
public void add()
{
[Link](a+ "+" +b+ "=" +(a+b));
}
}

//filename: [Link]
package nestedpackage.pack2; //
public class PackCode2
{
int a;
int b;
public PackCode2(int x, int y)
{

Page 5
Packages & Multithreading

a=x;
b=y;
}
public void multiply()
{
[Link](a+ "x" +b+ "=" +(a*b));
}
}

// importing nestedpackage and pack2 packages.


//filename: [Link]
import nestedpackage.*;
import nestedpackage.pack2.*;
import [Link].*;

public class AddMul


{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any two numbers:");
int m=[Link]();
int n=[Link]();
[Link](“Sum and Product of your two numbers is:”);
PackCode1 pc1=new PackCode1(m,n);
[Link]();
PackCode2 pc2=new PackCode2(m,n);
[Link]();
}
}

Output:
Enter any two numbers:
25
25
Sum and Product of your two numbers is:
25+25=50
25x25=625

Page 6
Packages & Multithreading

Built-in Packages
Java is rich of built-in packages. The following is the list of built-in packages.

PACKAGE
EXAMPLE CLASSES FUNCTIONALITY (PURPOSE)
NAME
These classes are indispensable
for every Java program. For this
System, String, Object, Thread,
[Link] reason, even if this package is not
Exception etc.
imported, JVM automatically
imports.
These are called as utility
[Link] (service) classes and are used
very frequently in coding.
FileInputStream,
FileOutputStream, FileReader, These classes are used in all I/O
[Link] FileWriter, RandomAccessFile, operations including keyboard
BufferedReader, BufferedWriter input.
etc.
URL, ServerSocket, Socket, Useful for writing socket
[Link] DatagramPacket, programming (LAN
DatagramSocket etc. communication).
Required for developing applets
AppletContext, Applet,
[Link] that participate on client-side in
AudioStub, AudioClip etc
Internet (Web) programming.
Button, Choice, TextField, Essential for developing GUI
[Link]
Frame, List, Checkbox etc. applications.
MouseListener, ActionListener, Without these classes, it is
java. awt.
ActionEvent, WindowAdapter impossible to handle events
event
etc. generated by GUI components
DriverManager, Statement, Required for database access in
[Link]
Connection, ResultSet etc JDBC applications.

Table: Some Predefined packages in java


How to put two public classes in a package?
If you want to put two public classes in a same package, create two java source files
containing one public class, but keep the package name same.
For example:
//save as [Link] //save as [Link]
package college; package college;
public class A{ } public class B{ }

Therefore the classes A and B will be in same package “college”.

Page 7
Packages & Multithreading

MULTI THREADED PROGRAMMING


Introduction
Java provides built-in support for multithreaded programming. A multi-threaded
program contains two or more parts that can run concurrently and each part can handle
a different task at the same time making optimal use of the available resources.
Multi tasking:
Multitasking is a concept of performing multiple tasks (also known as processes)
over a certain period of time by executing them concurrently.

Multi threading:
It is the process of implementing two or more parts of a program to run
concurrently. Each part of such a program is called a thread, and each thread defines a
separate path of execution, thus multithreading is a specialized form of multitasking.

Types of Multi tasking:

Multitasking is a process of executing multiple tasks simultaneously. We use


multitasking to utilize the CPU.
Multitasking can be achieved by two ways:

 Process-based Multitasking(Multiprocessing)
 Thread-based Multitasking(Multithreading)

Process-based Multitasking
 Each process has its own address in memory i.e. each process allocates separate
memory area.
 Process is heavyweight.
 Cost of communication between the process is high.
 Switching from one process to another require some time for saving and loading
registers, memory maps, updating lists etc.
 For example, process-based multitasking enables you to run the Java compiler at
the same time that you are using a text editor.

Thread-based Multitasking
 Threads share the same address space.

Page 8
Packages & Multithreading

 Thread is lightweight.
 Cost of communication between the thread is low.
 If you have programmed for operating systems such as Windows, then you are
already familiar with multithreaded programming.
 For instance, a text editor can format text at the same time that it is printing, as
long as these two actions are being performed by two separate threads.

Java Thread Model


Thread:
A thread is defined as a light-weight process that shares the same address space.
Life Cycle of a Thread (Thread States):
A thread can be in one of the five states. The life cycle of the thread in java is controlled
by JVM.
The java thread states are as follows:

1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated

Page 9
New Thread New Born Interfaces, Packages & Multithreading

stop
start

stop Dead
Active State
Running Runnable
Thread

yield

Suspend stop
resume
Sleep
Wait notify

Idle Thread
(Not Runnable) Blocked

Fig: 4.2 Life Cycle of a Thread

Following are the stages of the life cycle –

1) New
The thread is in new state if you create an instance of Thread class but before the
invocation of start() method.
2) Runnable
The thread is in runnable state after invocation of start() method, but the thread
scheduler has not selected it to be the running thread.
3) Running
The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not eligible to run.
5) Terminated
A thread is in terminated or dead state when its run() method exits.

How to create thread

There are two ways to create a thread:

Srinivasa Ramanujan Institute of Technology Page 10


Interfaces, Packages & Multithreading

1. By extending Thread class

2. By implementing Runnable interface.

Thread class:

Thread class provide constructors and methods to create and perform operations on a
thread. Thread class extends Object class and implements Runnable interface.

Commonly used Constructors of Thread class:

 Thread()
 Thread(String name)
 Thread(Runnable r)
 Thread(Runnable r,String name)

Commonly used methods of Thread class:

1. public void run(): is used to perform action for a thread.


2. public void start(): starts the execution of the thread. JVM calls the run()
method on the thread.
3. public void sleep(long milliseconds): Causes the currently executing thread to
sleep (temporarily cease execution) for the specified number of milliseconds.
4. public void join(): waits for a thread to die.
5. public void join(long milliseconds): waits for a thread to die for the specified
milliseconds.
6. public int getPriority(): returns the priority of the thread.
7. public int setPriority(int priority): changes the priority of the thread.
8. public String getName(): returns the name of the thread.
9. public void setName(String name): changes the name of the thread.
10. public Thread currentThread(): returns the reference of currently executing
thread.
11. public int getId(): returns the id of the thread.
12. public [Link] getState(): returns the state of the thread.
13. public boolean isAlive(): tests if the thread is alive.
14. public void yield(): causes the currently executing thread object to temporarily
pause and allow other threads to execute.
15. public void suspend(): is used to suspend the thread(depricated).
16. public void resume(): is used to resume the suspended thread(depricated).
17. public void stop(): is used to stop the thread(depricated).

Srinivasa Ramanujan Institute of Technology Page 11


Interfaces, Packages & Multithreading

18. public void interrupt(): interrupts the thread.


19. public boolean isInterrupted(): tests if the thread has been interrupted.
20. public static boolean interrupted(): tests if the current thread has been
interrupted.

The Main Thread

When a Java program starts up, one thread begins running immediately. This is
usually called the main thread of the program, because it is the one that is executed
when the program begins.
The main thread is important for two reasons:
• It is the thread from which other “child” threads will be spawned.
• Often, it must be the last thread to finish execution because it performs various
shutdown actions.
Although the main thread is created automatically when the program is started, it can
be controlled through a Thread object. To do so, we must obtain a reference to it by
calling the method currentThread( ), which is a public static member of Thread.
Its general form is shown here:
static Thread currentThread( )
It returns a reference to the thread in which it is called.
Program: Write a java program to illustrate Controlling the main Thread.
class CurrentThreadDemo {
public static void main(String args[]) {
Thread t = [Link]();
[Link]("Current thread: " + t);
// change the name of the thread
[Link]("My Thread");
[Link]("After name change: " + t);
try {
for(int n = 5; n > 0; n--) {
[Link](n);
[Link](1000);
}
} catch (InterruptedException e) {

Srinivasa Ramanujan Institute of Technology Page 12


Interfaces, Packages & Multithreading

[Link]("Main thread interrupted");


}
}
}

Output:
Current thread: Thread[main,5,main]
After name change: Thread[My Thread,5,main]
5
4
3
2
1

Create a Thread by Extending a Thread Class


To create a new thread, create a new class that extends Thread class using the
following two simple steps.
Step 1
We need to override run( ) method available in Thread class. This method provides an
entry point for the thread and we put the complete business logic inside this method.
Syntax : public void run( )
Step 2
Once Thread object is created, we can start it by calling start() method, which executes
a call to run( ) method.
Syntax: void start( );

Program: Write a java program to create threads by extending Thread class.


// Creating thread by extending Thread
class NewThread extends Thread {
NewThread() {
// Create a new thread by name Demo Thread
super("Demo Thread");
[Link]("Child thread: " + this);
start(); // Start the thread
}
// This is the entry point for the thread.
public void run() {
try {

Srinivasa Ramanujan Institute of Technology Page 13


Interfaces, Packages & Multithreading

for(int i = 5; i > 0; i--) {


[Link]("Child Thread: " + i);
[Link](500);
}
} catch (InterruptedException e) {
[Link]("Child interrupted.");
}
[Link]("Exiting child thread.");
}
}
class ExtendThread {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
[Link]("Main Thread: " + i);
[Link](1000);
}
} catch (InterruptedException e) {
[Link]("Main thread interrupted.");
}
[Link]("Main thread exiting.");
}
}

Output:
Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.

Create a Thread by implementing Runnable interface.


If your class is intended to be executed as a thread then you can achieve this by
implementing a Runnable interface. You will need to follow three basic steps −

Srinivasa Ramanujan Institute of Technology Page 14


Interfaces, Packages & Multithreading

Step 1
Implement a run() method provided by a Runnable interface. This method provides an
entry point for the thread and you will put your complete business logic inside this
method.
Following is a simple syntax of the run() method −
public void run( ) { }
Step 2
As a second step, you will instantiate a Thread object using the following constructor −
Thread(Runnable threadObj, String threadName);
Where, threadObj is an instance of a class that implements the Runnable interface
and threadName is the name given to the new thread.
Step 3
Once a Thread object is created, you can start it by calling start() method, which
executes a call to run( ) method.
Following is a simple syntax of start() method −
void start();
Program: Write a java program to create threads by implementing Runnable interface.
// Create a second thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
[Link]("Child thread: " + t);
[Link](); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
[Link]("Child Thread: " + i);
[Link](500);
}

Srinivasa Ramanujan Institute of Technology Page 15


Interfaces, Packages & Multithreading

} catch (InterruptedException e) {
[Link]("Child interrupted.");
}
[Link]("Exiting child thread.");
}
}
class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
[Link]("Main Thread: " + i);
[Link](1000);
}
} catch (InterruptedException e) {
[Link]("Main thread interrupted.");
}
[Link]("Main thread exiting.");
}
}
Output:
Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.

Srinivasa Ramanujan Institute of Technology Page 16


Interfaces, Packages & Multithreading

Creating Multiple Threads


Previously we had been used only two threads: the main thread and one child
thread. However, your program can spawn as many threads as it needs. For example,
the following program creates three child threads:
// Create multiple threads.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
[Link]("New thread: " + t);
[Link](); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
[Link](name + ": " + i);
[Link](1000);
}
} catch (InterruptedException e) {
[Link](name + "Interrupted");
}
[Link](name + " exiting.");
}
}
class MultiThreadDemo {
public static void main(String args[]) {
new NewThread("One"); // start threads
new NewThread("Two");
new NewThread("Three");

Srinivasa Ramanujan Institute of Technology Page 17


Interfaces, Packages & Multithreading

try {
// wait for other threads to end
[Link](10000);
} catch (InterruptedException e) {
[Link]("Main thread Interrupted");
}
[Link]("Main thread exiting.");
}
}

Output:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Three: 3
Two: 3
One: 2
Three: 2
Two: 2
One: 1
Three: 1
Two: 1
One exiting.
Two exiting.

Srinivasa Ramanujan Institute of Technology Page 18


Interfaces, Packages & Multithreading

Three exiting.
Main thread exiting.
Thread Priorities
Thread priorities are used by the thread scheduler to decide when each thread
should be allowed to run. The higher-priority threads get more CPU time than lower-
priority threads.

A higher-priority thread can also preempt (forceful termination) a lower-priority


one. Threads of equal priority should get equal access to the CPU.

For instance, when a lower-priority thread is running and a higher-priority thread


resumes (from sleeping or waiting on I/O, for example), it will preempt the lower
priority thread.

To set a thread’s priority, use the setPriority( ) method, which is a member of


Thread.

Syntax: final void setPriority(int level)


Here, level specifies the new priority setting for the calling thread. The
value of level must be within the range MIN_PRIORITY and. Currently, the priority
levels are as follows:

public static MIN_PRIORITY 1


public static NORM_PRIORITY 5
public static MAX_PRIORITY 10
These priorities are defined as static final variables within Thread.

Example: t1. setPriority(Thread. MAX_PRIORITY); // t1’s priority is set to 10


t2. setPriority(Thread. NORM_PRIORITY + 2); // t2’s priority is set to 7

Program: Write a java program to illustrate thread priorities.

class TestMultiPriority extends Thread


{

Srinivasa Ramanujan Institute of Technology Page 19


Interfaces, Packages & Multithreading

public void run()


{
[Link]("running thread name is:"+[Link]().getNam
e());
[Link]("running thread priority is:"+[Link]().getPri
ority());
}
public static void main(String args[])
{
TestMultiPriority m1=new TestMultiPriority( );
TestMultiPriority m2=new TestMultiPriority( );
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MAX_PRIORITY);
[Link]();
[Link]();
}
}
Output:
running thread name is:Thread-0
running thread priority is:10
running thread name is:Thread-1
running thread priority is:1

Srinivasa Ramanujan Institute of Technology Page 20


Interfaces, Packages & Multithreading

Using isAlive( ) and join( )


To determine whether a thread has finished, two ways are exist.
First, you can call isAlive( ) on the thread. This method is defined by Thread.
General form: final boolean isAlive( )
The isAlive( ) method returns true if the thread upon which it is called is still
running. It returns false otherwise.
Second, While isAlive( ) is occasionally useful, the method that you will more
commonly use to wait for a thread to finish is called join( ).
General form: final void join( ) throws InterruptedException
This method waits until the thread on which it is called terminates. Its name comes
from the concept of the calling thread waiting until the specified thread joins it.

Program: Write a java program to illustrate the use of isAlive() and join() methods.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
[Link]("New thread: " + t);
[Link](); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
[Link](name + ": " + i);
[Link](1000);
}
} catch (InterruptedException e) {
[Link](name + " interrupted.");
}
[Link](name + " exiting.");
}
}
class DemoJoin {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");

Srinivasa Ramanujan Institute of Technology Page 21


Interfaces, Packages & Multithreading

[Link]("Thread One is alive: " + [Link]());


[Link]("Thread Two is alive: " + [Link]());
[Link]("Thread Three is alive: " + [Link]());
// wait for threads to finish
try {
[Link]("Waiting for threads to finish.");
[Link]();
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]("Main thread Interrupted");
}
[Link]("Thread One is alive: " + [Link]());
[Link]("Thread Two is alive: " + [Link]());
[Link]("Thread Three is alive: " + [Link]());
[Link]("Main thread exiting.");
}
}

Output:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Two: 1
Three: 1
Two exiting.

Srinivasa Ramanujan Institute of Technology Page 22


Interfaces, Packages & Multithreading

Three exiting.
One exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.

Thread Synchronization
When two or more threads need access to a shared resource, they need some
way to ensure that the resource will be used by only one thread at a time. The process
by which this is achieved is called synchronization.
The synchronization is mainly used to
1. To prevent thread interference.
2. To prevent inconsistency problem.
Principle: The key principle of synchronization is the concept of the monitor.
Monitor:
A monitor is an object that is used as a mutually exclusive lock, or mutex. Only
one thread can own a monitor at a given time. When a thread acquires a lock, it is said to
have entered the monitor. All other threads attempting to enter the locked monitor will
be suspended until the first thread exits the monitor. These other threads are said to be
waiting for the monitor.
Using Synchronized Methods
To enter an object’s monitor, just call a method that has been modified with the
synchronized keyword. While a thread is inside a synchronized method, all other
threads that try to call it (or any other synchronized method) on the same instance have
to wait. To exit the monitor and relinquish control of the object to the next waiting
thread, the owner of the monitor simply returns from the synchronized method.
Example:
Suppose two different threads are performing two operations writing and
reading a same file at a time. The file should be available to one thread at a time. Hence
the two methods, on which the threads are trying to access a file, must be synchronized.
synchronized public void write(char ch) {
;
}

Srinivasa Ramanujan Institute of Technology Page 23


Interfaces, Packages & Multithreading

synchronized public char read( ) {


;
;
}
Interthread Communication
In the above concept, the threads are unconditionally blocked other threads from
asynchronous access to certain methods.
Threads also provide a secondary benefit: they do away with polling. Polling is
usually implemented by a loop that is used to check some condition repeatedly. Once
the condition is true, appropriate action is taken. This wastes CPU time.
To avoid polling, Java includes an elegant inter-thread communication
mechanism via the wait( ), notify( ), and notifyAll( ) methods.
Inter-thread communication or Co-operation is all about allowing
synchronized threads to communicate with each other.
Cooperation (Inter-thread communication) is a mechanism in which a thread is
paused running in its critical section and another thread is allowed to enter (or lock) in
the same critical section to be executed. It is implemented by following methods.
These methods are implemented as final methods in Object, so all classes have them.
Note: All three methods can be called only from within a synchronized context.
1. wait() method
Causes current thread to release the lock and wait until either another thread
invokes the notify() method or the notifyAll() method for this object, or a specified
amount of time has elapsed.

Method Description
public final void wait() throws
waits until object is notified.
InterruptedException
public final void wait(long timeout) throws waits for the specified amount
InterruptedException of time.

2. notify( ) method
Wakes up a single thread that is waiting on this object's monitor. If any threads
are waiting on this object, one of them is chosen to be awakened.
Syntax: public final void notify()

Srinivasa Ramanujan Institute of Technology Page 24


Interfaces, Packages & Multithreading

3. notifyAll( ) Method
Wakes up all threads that are waiting on this object's monitor.
Syntax: public final void notifyAll( )

Program: Write a java program that correctly implements producer-consumer problem using
the concept of inter thread communication.
Source Code:
class Q
{
boolean valueSet=false;
int n;
synchronized int get()
{
if(!valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
[Link]("Exception is:"+e);
}
[Link]("got:"+n);
valueSet = false;
notify();
return n;
}
synchronized void put(int n)
{
if(valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
[Link]("\n Exception in put:"+e);
}
this.n=n;
valueSet=true;
[Link]("\n put:"+n);
notify();
}
}
class Producer implements Runnable
{

Srinivasa Ramanujan Institute of Technology Page 25


Interfaces, Packages & Multithreading

Q q;
Producer(Q q)
{
this.q=q;
new Thread(this,"Producer").start();
}
public void run()
{
int i=0;
while(true)
[Link](i++);
}
}
class Consumer implements Runnable
{
Q q;
Consumer(Q q)
{
this.q=q;
new Thread(this,"Consumer").start();
}
public void run()
{
while(true)
[Link]();
}
}
class ProducerConsumer
{
public static void main(String args[])
{
Q q=new Q();
new Producer(q);
new Consumer(q);
}
}

Output:
put: 1
got: 1
put: 2
got: 2
put: 3
got: 3
put: 4
got: 4
put: 5
got: 5

Srinivasa Ramanujan Institute of Technology Page 26


Interfaces, Packages & Multithreading

Suspending, Resuming, and Stopping Threads

Sometimes, suspending execution of a thread is useful.


For example, a separate thread can be used to display the time of day. If the user doesn’t
want a clock, then its thread can be suspended.
We can use suspend( ) and resume( ), which are methods defined by Thread, to pause
and restart the execution of a thread.
General Form:
final void suspend( );
final void resume( );
Program: Write a java program to illustrate the use suspend, resume methods in
multithreading.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
[Link]("New thread: " + t);
[Link](); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 15; i > 0; i--) {
[Link](name + ": " + i);
[Link](200);
}
} catch (InterruptedException e) {
[Link](name + " interrupted.");
}
[Link](name + " exiting.");
}
}

Srinivasa Ramanujan Institute of Technology Page 27


Interfaces, Packages & Multithreading

class SuspendResume {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
try {
[Link](1000);
[Link]();
[Link]("Suspending thread One");
[Link](1000);
[Link]();
[Link]("Resuming thread One");
[Link]();
[Link]("Suspending thread Two");
[Link](1000);
[Link]();
[Link]("Resuming thread Two");
} catch (InterruptedException e) {
[Link]("Main thread Interrupted");
}
// wait for threads to finish
try {
[Link]("Waiting for threads to finish.");
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]("Main thread Interrupted");
}
[Link]("Main thread exiting.");
}
}

Srinivasa Ramanujan Institute of Technology Page 28


Interfaces, Packages & Multithreading

Output:
New thread: Thread[One,5,main]
One: 15
New thread: Thread[Two,5,main]
Two: 15
One: 14
Two: 14
One: 13
Two: 13
One: 12
Two: 12
One: 11
Two: 11
Suspending thread One
Two: 10
Two: 9
Two: 8
Two: 7
Two: 6
Resuming thread One
Suspending thread Two
One: 10
One: 9
One: 8
One: 7
One: 6
Resuming thread Two
Waiting for threads to finish.
Two: 5
One: 5
Two: 4
One: 4
Two: 3
One: 3
Two: 2
One: 2
Two: 1
One: 1
Two exiting.
One exiting.
Main thread exiting.

Srinivasa Ramanujan Institute of Technology Page 29

You might also like