Unit3 OOPS
Unit3 OOPS
Unit3 OOPS
Course WorkBook-Unit-3
3 Assignment 46
Exception Handling basics- Multiple catch Clauses – Nested try statements-Java’s Built-in Exceptions
– User defined Exception-Multithreaded Programming: Java Thread Model- Creating a Thread and
Multiple Threads-Inter Thread Communication-Suspending-Resuming, and Stopping Threads–
Multithreading - Wrappers – Auto boxing
1. What happens when the statement: int value = 25/0; is executed? (MAY/JUN 2016,
APR/MAY 2017, APR/MAY 2019)
2. Define runtime exceptions. (NOV/DEC 2018)
3. What is the use of assert keyword? (NOV/DEC 2018, NOV/DEC 2018)
4. Explain the types of exceptions in java. (NOV/DEC 2019)
5. Write a Java program to create user define exception. (APR/MAY 2017)
6. What is exception handling? Explain with an example exception handling in java.
(APR/MAY 2018)
7. Explain ‘Divide by zero’ Exception with an example. Java program. (APR/MAY 2019)
8. State the use of try block in Java exception handling. (NOV/DEC 2017)
9. Throw an exception if the matrices cannot be multiplied and handle it using a user
defined exception. (NOV/DEC 2017)
10. Discuss in detail on how exceptions are handled with an appropriate example.
(NOV/DEC 2019)
Errors indicate serious problems and abnormal conditions that most applications should not try to
handle. Error defines problems that are not expected to be caught under normal circumstances by
our program. For example memory error, hardware error, JVM error etc.
Exceptions are conditions within the code. A developer can handle such conditions and take
necessary corrective actions. Few examples
• DivideByZero exception
• NullPointerException
• ArithmeticException
• ArrayIndexOutOfBoundsException
An exception (or exceptional event) is a problem that arises during the execution of a program. When
an Exception occurs the normal flow of the program is disrupted and the program/Application
terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. If
an exception is raised, which has not been handled by programmer then program execution can get
terminated and system prints a non-user-friendly error message.
Ex: Exception in thread "main"
java.lang.ArithmeticException: / by zero at ExceptionDemo.main
(ExceptionDemo.java:5)
ExceptionDemo :
The class name main : The method name
ExceptionDemo.java : The filename
java: 5: Line number
An exception can occur for many different reasons. Following are somescenarios where an exception
occurs.
• A user has entered an invalid data.
• A network connection has been lost in the middle of communications orthe JVM has run out
of memory.
All exception classes are subtypes of the java.lang.Exception class. Theexception class is a subclass
of the Throwable class.
1. try A try/catch block is placed around the code that might generate an
to catch.
3. finally A finally block of code always executes, irrespective of occurrence of an
Exception.
4. throw It is used to execute important code such as closing connection, stream
Output
Exception thrown :
java.lang.ArrayIndexOutOfBoundsException:3
First element value: 6
The finally statement is executed
Note : here array size is 2 but we are trying to access 3rd
element.
Uncaught Exceptions
This small program includes an expression that intentionally causes a divide-by- zero error:
class Exc0 {
public static void main(String args[])
{
int d = 0;
int a = 42 / d;
}}
When the Java run-time system detects the attempt to divide by zero, it constructs a new exception
object and then throws this exception. This causes the execution of Exc0 to stop, because once an
exception has been thrown, it must be caught by an exception handler and dealt with immediately
Any exception that is not caught by your program will ultimately be processed by the default handler.
The default handler displays a string describing the exception, prints a stack trace from the point at
which the exception occurred, and terminates the program.
Stack Trace
Stack Trace is a list of method calls from the point when the application was started to the
point where the exception was thrown. The most recent method calls are at the top. A stacktrace is a
very helpful debugging tool. It is a list of the method calls that the application was in the middle of
when an Exception was thrown. This is very useful because it doesn't only show you where the error
happened, but also how the program ended up in that place of the code. Using try and Catch to guard
against and handle a run-time error, simply enclose the code that you want to monitor inside a try
block. Immediately following the try block, include a catch clause that specifies the exception type
that you wish to catch. A try and its catch statement form a unit. The following program includes a try
block and a catch clause that processes the ArithmeticException generated by the division-by-zero
error
class Exc2
{
public static void main(String args[])
{
int d, a;
try {
// monitor a block of code.
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
} catch (ArithmeticException e)
{ // catch divide-by-zero error
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}
This program generates the following output: Division by zero. After catch statement. The call to
println( ) inside the try block is never executed. Once an exception is thrown, program control
transfers out of the try block into the catch block.
Assertion
Assertion is a statement in java. It can be used to test your assumptions about the program. While
executing assertion, it is believed to be true. If it fails, JVM will throw an error named AssertionError.
It is mainly used for testing purpose.
Advantage of Assertion:
import java.util.Scanner;
class AssertionExample{
public static void main( String args[] ){
System.out.println("value is "+value);
}
}
Output:
Enter ur age 11
Exception in thread "main" java.lang.AssertionError: Not valid
Categories of Exceptions
Checked exceptions −A checked exception is an exception that occurs at the compile time, these are
also called as compile time exceptions. These exceptions cannot simply be ignored at the time of
compilation, the programmer should take care of (handle) these exceptions.
2) Scenario where NullPointerException occurs If we have null value in any variable, performing any
operation by the variable occurs an NullPointerException.
String s=null; System.out.println(s.length());//NullPointerException
3) Scenario where ArrayIndexOutOfBoundsException occurs If you are inserting any value in the
wrong index, it would result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
class MultiCatch
{
public static void main(String args[])
{
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
}
catch(ArithmeticException e)
{
System.out.println("Divide by 0: " + e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index oob: " + e);
}
System.out.println("After try/catch blocks."); } }
Here is the output generated by running it both ways:
C:\>java MultiCatch
a=0
Divide by 0: java.lang.ArithmeticException: / by zero After try/catch blocks.
C:\>java MultiCatch TestArg
a=1
Array index oob: java.lang.ArrayIndexOutOfBoundsException:42 After try/catch blocks.
The try statement can be nested. That is, a try statement can be inside the block of another try.
Each time a try statement is entered, the context of that exception is pushed on the stack. If an inner
try statement does not have a catch handler for a particular exception, the stack is unwound and the
next try statement’s catch handlers are inspected for a match. This continues until one of the catch
statements succeeds, or until all of the nested try statements are exhausted. If no catch statement
matches, then the Java run-time system will handle the exception.
C:\>java NestTry
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One
a=1 Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One Two
a=2 Array index out-of-bounds: java.lang.ArrayIndexOutOfBoundsException:42
throw
it is possible for your program to throw an exception explicitly, using the throw statement. The
general form of throw is shown here:
throw ThrowableInstance;
throws
If a method is capable of causing an exception that it does not handle, it must specify this
behaviour so that callers of the method can guard themselves against that exception. You do this by
including a throws clause in the method’s declaration. A throws clause lists the types of exceptions
that a method might throw. This is necessary for all exceptions, except those of type Error or
RuntimeException, or any of their subclasses. All other exceptions that a method can throw must be
declared in the throws clause.
This is the general form of a method declaration that includes a throws clause:
type method-name(parameter-list) throws exception-list
{ // body of method }
Here, exception-list is a comma-separated list of the exceptions that a method can throw.
class ThrowsDemo {
static void throwOne() throws IllegalAccessException
{
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{ try { throwOne(); }
catch (IllegalAccessException e) {
System.out.println("Caught " + e); } } }
finally
The finally keyword is designed to address this contingency. finally creates a block of code that will
be executed after a try/catch block has completed and before the code following the try/catch block.
The finally block will execute whether or not an exception is thrown. If an exception is thrown, the
finally block will execute even if no catch statement matches the exception. Any time a method is
about to return to the caller from inside a try/catch block, via an uncaught exception or an explicit
return statement, the finally clause is also executed just before the method returns. This can be useful
for closing file handles and freeing up any other resources that might have been allocated at the
beginning of a method with the intent of disposing of them before returning. The finally clause is
optional.
// Demonstrate finally.
class FinallyDemo {
// Through an exception out of the method.
Static void procA()
{
try
{ System.out.println("inside procA");
throw new RuntimeException("demo");
}
finally {
System.out.println("procA's finally"); } }
// Return from within a try block.
static void procB()
{
try {
System.out.println("inside procB");
return;
} finally { System.out.println("procB's finally"); } }
// Execute a try block normally.
static void procC()
{
try { System.out.println("inside procC"); }
finally { System.out.println("procC's finally"); } }
public static void main(String args[]) {
try { procA(); }
catch (Exception e) { System.out.println("Exception caught");
}
procB();
procC();
}
}
1 ArithmeticException
Arithmetic error, such as divide-by-zero.
2 ArrayIndexOutOfBoundsException
Array index is out-of-bounds.
3 ArrayStoreException
Assignment to an array element of an incompatible type.
4 ClassCastException
Invalid cast.
5 IllegalArgumentException
Illegal argument used to invoke a method.
6 IllegalMonitorStateException
Illegal monitor operation, such as waiting on an unlocked thread.
7 IllegalStateException
Environment or application is in incorrect state.
8 IllegalThreadStateException
Requested operation not compatible with the current thread state.
9 IndexOutOfBoundsException
Some type of index is out-of-bounds.
10 NegativeArraySizeException
Array created with a negative size.
11 NullPointerException
Invalid use of a null reference.
12 NumberFormatException
Invalid conversion of a string to a numeric format.
13 SecurityException
Attempt to violate security.
14 StringIndexOutOfBounds
Attempt to index outside the bounds of a string.
15 UnsupportedOperationException
An unsupported operation was encountered.
1 ClassNotFoundException
Class not found.
2 CloneNotSupportedException
Attempt to clone an object that does not implement the Cloneable interface.
3 IllegalAccessException
Access to a class is denied.
4 InstantiationException
Attempt to create an object of an abstract class or interface.
5 InterruptedException
One thread has been interrupted by another thread.
6 NoSuchFieldException
A requested field does not exist.
7 NoSuchMethodException
A requested method does not exist.
User defined exception needs to inherit (extends) Exception class inorder to act as an
exception throw keyword is used to throw such exceptions.
super(msg);
}
class EmployeeTest
{
{
try
{
employeeAge(-2);
catch (MyOwnException e)
{
e.printStackTrace();
}}}
• Exception handling allows us to control the normal flow of the program by using exception
handling in program.
• It throws an exception whenever a calling method encounters an error providing that the
calling method takes care of that error.
• It also gives us the scope of organizing and differentiating between different error types using
a separate block of codes. This is done with thehelp of try-catch blocks.
2. MULTITHREADED PROGRAMMING
Thread
Multithreading
Multithreading is a conceptual programming concept where a program (process)is divided into two
or more subprograms (process), which can be implemented at the same time in parallel. A
multithreaded program contains two or more partsthat can run concurrently. Each part of such a
program is called a thread, and each thread defines a separate path of execution.
Multitasking
Thread-based multi-tasking
Executing several tasks simultaneously where each task is a separate independent part of the same
program is called Thread-based multitasking and each independent part is called Thread. It is best
suitable for the programmatic level. The main goal of multi-tasking is to make or do a better
performance of the system by reducing response time
Multithreading vs Multitasking
Life Cycle of Thread - A thread can be in any of the five following states
1) Newborn State
When a thread object is created a new thread is born and said to be in Newborn state.
2) Runnable State
If a thread is in this state it means that the thread is ready for execution and waiting for the
availability of the processor. If all threads in queue are of same priority, then they are given
time slots for execution in round robin fashion.
3) Running State
It means that the processor has given its time to the thread for execution. A thread keeps
running until the following conditions occurs.
Thread gives up its control on its own and it can happen in the following situations
• A thread gets suspended using suspend () method which can only be revived with resume()
method
• A thread is made to sleep for a specified period of time using sleep(time) method, where
time in milliseconds
• A thread is made to wait for some event to occur using wait () method. In this case a thread
can be scheduled to run again using notify ()method.
4) Blocked State
5) Dead State
A runnable thread enters the Dead or terminated state when it completes its task or
otherwise
class MainThread
{
Thread t1=Thread.currentThread();
t1.setName("MainThread");
Output:
Example:
public class ThreadSample implements Runnable
{
public void run()
{
try {
for (int i = 5; i > 0; i--)
{
System.out.println("Child Thread" + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e) {
System.out.println("Child interrupted");
}
System.out.println("Exiting Child Thread"); }
}
public class MainThread {
public static void main(String[] arg) {
ThreadSample d = new ThreadSample();
Thread s = new Thread(d);
s.start();
try
{
for (int i = 5; i > 0; i--)
{
System.out.println("Main Thread" + i);
Thread.sleep(5000);
}}
catch (InterruptedException e) {
System.out.println("Main interrupted"); }
System.out.println("Exiting Main Thread"); }}
The second way to create a thread is to create a new class that extends Thread, and then to
create an instance of that class. The extending class must override the run( ) method, which is the
entry point for the new thread. It must also call start( ) to begin execution of the new thread.
Example:
public class ThreadSample extends Thread {
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println("Child Thread" + i);
Thread.sleep(1000); }
} catch (InterruptedException e) {
System.out.println("Child interrupted"); }
System.out.println("Exiting Child Thread");
}}
public class MainThread {
public static void main(String[] arg) {
ThreadSample d = new ThreadSample(); d.start(); try {
for (int i = 5; i > 0; i--) {
System.out.println("Main Thread" + i);
Thread.sleep(5000); }
} catch (InterruptedException e) {
System.out.println("Main interrupted"); }
System.out.println("Exiting Main Thread"); }
}
Thread priority
Each thread have a priority. Priorities are represented by a number between 1 and 10. In most
cases, thread schedular schedules the threads according to their priority (known as preemptive
scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling
it chooses.
3 constants defined in Thread class:
public static int MIN_PRIORITY
public static int NORM_PRIORITY
public static int MAX_PRIORITY
Default priority of a thread is 5 (NORM_PRIORITY).
The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10.
Example :
public class MyThread1 extends Thread {
MyThread1(String s) {
super(s);
start(); }
public void run() { for(int i=0;i<5;i++) {
Thread cur=Thread.currentThread();
cur.setPriority(Thread.MAX_PRIORITY);
int p=cur.getPriority();
System.out.println("Thread Name"+Thread.currentThread().getName());
System.out.println("Thread Priority"+cur); }
}}
class MyThread2 extends Thread
{ MyThread2(String s)
{ super(s);
start(); }
public void run()
{ for(int i=0;i<5;i++) {
Thread cur=Thread.currentThread();
cur.setPriority(Thread.MIN_PRIORITY);
System.out.println(cur.getPriority());
int p=cur.getPriority(); System.out.println("Thread Name"+Thread.currentThread().getName());
System.out.println("Thread Priority"+cur);
}
}}
public class ThreadPriority { public static void main(String[] args) {
MyThread1 m1=new MyThread1("MyThread1");
MyThread2 m2=new MyThread2("MyThread2"); }
}
Synchronizing Threads
• Synchronization in java is the capability to control the access of multiple threads to any shared
resource.
• Java Synchronization is better option where we want to allow only one thread to access the
shared resource
General Syntax :
synchronized(object) { //statement to be synchronized }
Types of Synchronization
• Process Synchronization
• Thread Synchronization
There are two types of thread synchronization mutual exclusive and inter-thread communication.
1. Mutual Exclusive
• Synchronized method.
• Synchronized block.
• static synchronization.
Synchronized method
• If you declare any method as synchronized, it is known as synchronized method.
• Synchronized method is used to lock an object for any shared resource.
• When a thread invokes a synchronized method, it automatically acquires the lock for that
object and releases it when the thread completes its task.
• wait()
• notify()
• notifyAll()
wait() - tells calling thread to give up monitor and go to sleep until some other thread enters the
same monitor and call notify.
notify() - wakes up a thread that called wait() on same object.
notifyAll() - wakes up all the thread that called wait() on same object.
wait() sleep()
wait() method releases the lock sleep() method doesn't release the lock.
It is the method of Object class It is the method of Thread class
It is the non-static method It is the static method
It should be notified by notify() or notifyAll() after the specified amount of time, sleep is
methods completed.
Class Customer{
int amount=10000;
synchronized void withdraw(int amount){
System.out.println("going to withdraw...");
if(this.amount<amount){
System.out.println("Less balance; waiting for deposit...");
try{
wait();
}catch(Exception e){}
}
this.amount-=amount;
System.out.println("withdraw completed..."); }
synchronized void deposit(int amount)
{
System.out.println("going to deposit...");
this.amount+=amount;
System.out.println("deposit completed... ");
notify();
}
}
class Test{
public static void main(String args[]){
final Customer c=new Customer();
new Thread(){
public void run(){
c.withdraw(15000);
}
}.start();
new Thread(){
public void run()
{
c.deposit(10000);
}
}.start();
}}
Daemon thread in java is a service provider thread that provides services to the user thread. Its life
depend on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread
automatically.
Example:
2.4 Multithreading
isAlive() The isAlive method is invoked to verify if the thread is alive or dead
The yield method is used to send the currently executing threads to standby
yield()
mode and runs different sets of threads on higher priority
suspend() The suspend method is used to instantly suspend the thread execution
destroy() The destroy method is invoked to destroy the execution of a group of threads
3. WRAPPERS
1. Write a program to convert String from back to its data types using parseXXX()
methods?
2. Can a Byte object be cast to a double value?
3. Name the eight primitive Java types.
4. Why do we need wrapper classes?
5. What is autoboxing?
Wrapper classes in Java provides a way to wrap or represent the value of primitive data types as an
object. By creating an object to the wrapper class, a data field is created and, in this field, we can store
the value of a primitive data type. It also includes methods to unwrap the objects back into the
primitive data types. It is one of the classes provided in the java.lang package and all of the primitive
wrapper classes in Java are immutable.
• Primitive Data Types cannot be directly used as objects that's why Wrapper classes come into
picture.
• Generic classes work with objects and don't support Primitives. As a result, Wrapper classes
are needed as they convert primitive data types into objects and objects are really important
if we need to modify the arguments passed in a method. Now, let's discuss in detail about the
Wrapper Classes.
• The Java programming language provides the java.lang package which has classes that are
fundamental to the design and the most important classes among them are Object and Class.
• So, Java wrapper classes wraps or represents the values of primitive data types as an object.
When an object is created to a wrapper class, it contains a field which can store the primitive
data types.
• The object of one type contains a field of that particular type only, which means a Double type
of object contains double type of field only, representing that value so that a reference to it can
be stored in a variable of reference type.
Below are the Primitive Data Types and their corresponding Wrapper classes:
Primitive Data Type Wrapper Class
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
• Whenever the primitive types are required as an object, wrapper classes can be used.
• Wrapper classes also include methods to unwrap the object and give back the primitive data
type.
• In java.util package, the classes handle only objects and that's why in this case wrapper class
helps.
• In the Collection framework, Data Structures such as ArrayList stores only the objects and not
the primitive types
// Invalid
ArrayList<int> exampleList = new ArrayList<>();
// Valid
ArrayList<Integer> exampleList = new ArrayList<>();
• For the methods that support object like creation from other types such as String.
• Wrapper classes are also used for synchronization in multithreading. As objects are needed to
achieve the synchronization process where we ensure that the shared resource will be used
by only one thread at a time.
In Java Wrapper Classes, the object is created with fields or properties in which the primitive
data types can be stored.
Creating Wrapper Objects
Wrapper object can be created using the wrapper class and its constructor by passing the value to it.
Syntax:
Example:
We can create an instance of wrapper classes without their constructors as well. We can create a
variable of data type same as the class name of wrapper classes and assign a raw value to it without
using the new operator as shown below:
Syntax:
ClassName objectName = value;
Example:
public class CreatingWrapperObject {
Output:
10
8.89
S
Example: Converting an Integer to a String wrapper class object using the toString()method:
Output:
Syntax:
Example:
We have the 'call by value' function in Java programming, using this we can modify the arguments
passed into a method with the help of the objects converted from primitive data types. If the argument
is not constant and it needs to be modified, we can pass the objects and can modify the values
accordingly.
Synchronization
Synchronized blocks in Java are marked with the synchronized keyword. This block in Java is
synchronized on some object. All blocks that are synchronized on the same object can only have one
thread executing inside them at a time. All other threads attempting to enter the synchronized block
are blocked until the thread inside the synchronized block exits the block.
Serialization
To implement the serialization, the object is converted within streams. The object can be regrated
using the java wrapper classes. Basically, the class of the object must implement Serializable interact
directly or indirectly.
java.util package
The implementaton of the utility classes in the **java.util **package is to match with the objects and
wrapper classes help to achieve the same as well.
Collection framework
Java collection framework classes such as ArrayList, HashSet, Vector, LinkedList, etc. store only
objects i.e. reference types and not primitive types. So objects are instances of wrapper classes and
that's why it helps for this.
All of the numeric wrapper classes are subclasses of the abstract class Number such as Byte, Integer,
Double, Short, Float, Long. Some of the frequently used methods that all subclasses of the Number
class implements are listed in the following table:
The following methods are used to get the value associated with the corresponding wrapper object:
intValue(), byteValue(), shortValue(), longValue(), floatValue(), doubleValue(), charValue(),
booleanValue().
This example will output the same result as the example above:
Integer myInt = 5;
System.out.println(myInt.intValue());
System.out.println(myDouble.doubleValue());
System.out.println(myChar.charValue());
}}
There are more such methods which are implemented by the subclasses of the Number class. The
above table lists only few of them.
Output:
An object of Integer is created.
An object of Double is created.
Explanation:
In the above example, we have used the valueOf() method to convert the primitive types into objects.
Here, we have used the instanceof operator to check whether the generated objects are of Integer or
Double type or not.
However, the Java compiler can directly convert the primitive types into corresponding objects. For
example,
int a = 5;
// converts into object
Integer aObj = a;
double b = 5.6;
// converts into object
Double bObj = b;
This process is known as auto-boxing which we have already discussed. In which the conversion
happens for primitive types into objects internally. Like here in the above example, the primitive type
value of a is converted into object aObj. Internally, the Integer.valueOf(a) is used by the compiler and
aObj is assigned to the value of a that is 5.
class Main {
public static void main(String[] args) {
Output:
The value of a: 23
The value of b: 5.55
Explanation:
In the above example, we have used the intValue() and doubleValue() method to convert the Integer
and Double objects into corresponding primitive types.
However, the Java compiler can automatically convert objects into corresponding primitive types. For
example,
SpeedWrapperClass() {}
SpeedWrapperClass(int speed) {
this.speed = speed;
}
@Override
public String toString() {
return Integer.toString(speed);
}
}
100
Explanation:
Here, we have created a custom wrapper class that is SpeedWrapperClass() and to set the value of
speed we have also created constructors.
Now, when we created an instance of the custom wrapper class and passed the integer value it worked
as Integer wrapper class which wraps the int value of 100 and set the speed as 100.
In a way, we have a data member field of type int speed which holds the primitve int value passed as
argument to the constructor.
Advantages of Java Wrapper Class in Java
In Java, there can be various scenarios where we need to use objects instead of primitive data types.
For example, while working with collections. As we can see in the below example we need to use
Integer not the primitive int.
// error
ArrayList<int> list = new ArrayList<>();
// runs perfectly
ArrayList<Integer> list = new ArrayList<>()
We can use the objects of the wrapper class and store them as a null value. In real time scenario
applications, the values can be null and hence, we need to store null values.
Objects of classes such as Character and Integer are pointers: the actual number stored in the bytes
that are that reference variable's value represents an address in memory. So, it is possible and
meaningful to set that number to an address that goes nowhere. This is exactly what null is. A
primitive like int or char, however, has a number that is interpreted as a number (integer, or ASCII
code), and there's no way to make it "not a number" because all that the memory can possibly store
is numbers.
// generates an error
int a = null;
// runs perfectly
// More intuitive in real world applications
Integer a = null;
A wrapper type allows a primitive to operate in more advanced ways. An integer can be used in
various ways. For example, we can create a class Hour which will give a meaning to the number
associated with it.
The primitive types just operate with value, the wrapper class provides it with a name. For example,
int as Integer, char as Character, etc. It means that int only specifies the range and type of the value
but by creating the object with wrapper class Integer it will be given a reference variable which points
to the object in the heap memory.
3.3 Autoboxing
Autoboxing is when the Java compiler performs the automatic conversion of the primitive data types
to the object of their corresponding wrapper classes. For example, converting an int to Integer, a
double to Double, etc.
The Java compiler applies autoboxing when a primitive value is:
Passed as a parameter to a method that expects an object of the corresponding wrapper class.
Assigned to a variable of the corresponding wrapper class.
For example:
//Autoboxing example of int to Integer and char to Char
public class AutoboxingExample {
public static void main(String args[]) {
char ch = 's';
//Autoboxing- primitive to Character object conversion
Character s = ch;
int a = 50;
// Converting int into Integer explicitly
Integer first = Integer.valueOf(a);
// Autoboxing, now compiler will write Integer.valueOf(a)
// internally and hence, doesn't generate an error
Integer second = a;
System.out.println(a);
System.out.println(first);
System.out.println(second);
}
}
Output:
50
50
50
Explanation:
Here, the output is 50 for all as:
The ‘a’ variable is assigned to int value 50.
The first variable is assigned to the value of ‘a’ that is 50. Only in such a case, primitive data type int
is converted into Integer explicitly.
The second variable will also have the value of ‘a’ that is 50 as due to autoboxing the compiler
internally performs the conversion automatically (implicit conversion).
3.4 Unboxing
It is just the opposite process of autoboxing. Unboxing is automatically converting an object of a
wrapper type (Integer, for example) to its corresponding primitive (int) value.
The Java compiler applies unboxing when an object of a wrapper class is:
Passed as a parameter to a method that expects a value of the corresponding primitive type.
Assigned to a variable of the corresponding primitive type.
For example:
System.out.println(a);
System.out.println(first);
System.out.println(second);
}
}
Output:
5
5
5
Explanation:
JUnit
Starting from requirement and analysis to design & development till maintenance, every
phase should have an appropriate testing phase associated with it. Unit testing after
development is what is advisable to build a robust application and to have an optimized
code in place.
Unit testing is testing of a small logic or a code to verify that the output of the code is as
expected on the input of a specific data and/or on satisfying certain condition(s). Usually,
the unit tests are supposed to be independent of the other tests.
Unit tests are not feasible to test complex interfaces with another application or third
party/external services. A unit test targets only a small unit of code that could be just a
method or a class.
It helps the developer discover issues in the current logic and any regression failures due to
the current change. Besides, it also provides insight into how the current code could impact
future implementation.
Test Coverage
The percentage of code that is tested by unit tests is called test coverage.
The objective is to have better and more test coverage of the code which in future continues
to add up to the regression test suite and helps to increase automated test execution and
verification, thereby, reducing the manual effort involved in regression testing.
Many come with a question as to how much test coverage is essential. The answer to this
question is that there is no hard and fast rule to how much coverage of tests is essential; it
is all judgemental. The judgment gets better with experience on the application workflow
and historic knowledge of the defects found so far.
Efficient tests need not necessarily mean having 100% test coverage or incorporating
automation tests and/or unit tests for every single branch or path coverage.
Certain trivial verifications like a validation error message for a mandatory field left blank
that hasn’t flawed since years need not be included in the regression suite.
1. Manual testing
2. Automated testing
Automated Testing is preferred over Manual Testing for the below reasons:
When a testcase is executed manually When a testcase is executed with the help
without an intervention of a tool is called of a tool without much manual intervention
manual testing. is called automated testing.
Repetitive manual efforts are included. Repetitive manual efforts may be avoided.
Human efforts in manual testing could be Automation tests are faster and error free
erroneous and time consuming. compared to the manual efforts.
Testing resources required are more for Less testers are needed to execute
running every testcase manually thereby, automated tests using the designated
adding to the investment in the resources. automated tool(s) hence there is less
investment in testing resources thus
adding to the profitability.
Manual testing has to be limited to a small Many different test scenarios can be
test coverage considering the timeline automated and can be executed multiple
restrictions. Hence, there is a risk of times even under time and resource crisis
skipping many test scenarios thus leading hence leading to better test coverage and
to risk of defect leakage as well. better quality of the deliverable.
We may have the next question as to what does a typical automation unit test case looks
like and the framework it follows. The developers use the Unit Test framework for
creating automated unit test cases.
What Is JUnit?
JUnit is an open-source framework that is used for writing and executing unit tests in Java
programming language. It is one of the best-known unit testing frameworks.
The below image shows the different well-known automation unit testing tools.
Given below are the two examples of a very basic Hello World program to get an
understanding of how a JUnit test class looks like or how different does it look when
compared with a usual Java class file.
Example #1:
Here is a JUnit testcase HelloWorldJUnit.java that verifies that the string “Hello world”
matches the string “hello world” which fails on execution, as the match is case sensitive.
Hence, the two strings don’t match and the test fails.
package demo.tests;
import static org.junit.Assert.*;
import org.junit.Test;
public class HelloWorldJUnit {
@Test
public void test() {
assertEquals("Hello world","hello world");
}
}
Example #2:
Here, we will see how a usual Java class file interacts with a JUnit testcase. We create a
Java class file HelloWorld_Java.java with a constructor that allows us to pass a String value
and a method getText() to fetch the string value.
JUnit Test class HelloWorldJUnit.java is created such that the class object for
HelloWorld_Java is created and the actual string value is passed to the object. The
assertEquals() from JUnit verifies if the expected and actual string values match.
package demo.tests;
public class HelloWorldJUnit{
private String s;
public HelloWorld_Java(String s)
{
@Test
public void test() {
HelloWorld_Java hw=new HelloWorld_Java("Hello World");
assertEquals(hw.getText(),"Hello World");
}
}
The resultant looks like below where we see the two strings match. Hence, JUnit test is
passed.
OOP - Unit-3 - Assignment
Level-1
Q:
Write a Java program which handles Push operation and Pop operation on stack
concurrently.
Level-2
Q:
Write a Java program which first generates a set of random numbers and then
determines negative, positive even, positive odd numbers concurrently.
Level-3
Q:
Branch : CSE
Sub Code : CS3391 Year/Sem: II/III
Sub Name : Object Oriented Programming Common to:CSBS
Part A
Sl.no Questions Blooms
CO
Level*
1 CO3
Sketch the exception class hierarchy B3
2 CO3
Write a program to read multiple Integer values from a B3
3 CO3
Enumerate the difference between throw and throws
B1
keyboard
4 CO3
Define uncaught exceptions. B1
5 CO3
List the advantages of using exception handling. B1
6 CO3
Justify the purpose of the finally clause of a B5
try-catch-finally statement.
7 CO3 B3
Sketch the different states in thread.
12 CO3 B1
List the methods used for inter thread communication.
13 CO3 B1
Enumerate the difference between yielding and sleeping of
threads with an example.
Part B
Sl.no Blooms
CO
Questions Level*
1 CO3 B5
Summarize the following with example program
i.Arithmetic exception
ii.Arrayoutofbound exception
2 CO3 B2
Describe in detail about multi thread programming with
examples.
3 CO3 B3
Write a Java Program to explain the concept of User
Defined Exception.
4 CO3 B4
Explain the different types of exceptions and the exception
hierarchy in java with appropriate examples.
5 Explain inter thread communication in Java. CO3 B4
6 Create two threads and assign names ‘Scooby’ and CO3 B6
Multithreading.
Part C
Sl.no Blooms
CO
Questions Level*
1 CO3 B6
Create a simple real life application program in java
to illustrate the use of Multithreading.
2 CO3 B4
Illustrate the concept of Synchronization in Java
using the method static synchronization.
3 CO3 B6
Develop a Java Program to create a deadlock
between two threads.
Multithreading - MCQ
a.volatile
b.synchronized
c.locked
d.none
a.1
b.2
c.multiple
d.none
a.Thread
b.Process
c.JVM
d.
a.Processes have their own copy of the data segment of the parent process
b.Threads have direct access to the data segment of its process
c.Processes have their own address
d.All of these
a.StringBuffer
b.StringBuilder
c.All
d.None
a.New
b.Runnable
c.sleep
d.terminated
a.object
b.class
c.All
d.None
What state does Thread enter in when it has been created and started?
a.New
b.Runnable
c.Running
d.Waiting
Which method can be used to find whether Thread hasn't entered dead state?
a.isAlive()
b.isRunning()
c.isNotDead
d.All
How can you ensure all threads that started from main must end in order in which they
started and also main should end in last
a.join() method
b.sleep() method
c.wait() method
d.run() method
a.start()
b.restart()
c.restartThread()
d.none
What is true about acquiring object lock before calling wait(), notify() and notifyAll()?
a.it’s mandatory to acquire object lock before calling wait(), notify() and notifyAll()
methods on object
b.If we call wait(), notify() and notifyAll() methods without acquiring object lock i.e. from
outside synchronize block then java.lang.IllegalMonitorStateException is thrown at
runtime.
c.wait(), notify() and notifyAll() methods are always called from Synchronized block only
d.all
What is difference between starting thread with run() and start() method?
a.There is no difference
b.When you call start() method, main thread internally calls run() method to start newly
created Thread
c.run() calls start() method internally
d.None
a.notify/notifAll
b.When sleep time is up
c.Using resume() method when thread was suspended
d.All
MCQ - Exception Handling
Question 1
The closest common ancestor of RuntimeException, Error, IOException and
ClassNotFoundException is:
Object
Exception
Throwable
Catchable
Question 2
A method that potentially generates a checked exception must include this keyword in its
method signature:
throw
extend
throws
extends
Question 3
Which of the following statements is true about Java's finally block?
The finally block is only executed if an exception is thrown in the try block
The finally block is only executed if an exception is thrown in the catch block
The finally block is only executed if an exception is not thrown in the try or catch block
The finally block is executed regardless of whether an exception is thrown in the try or
catch block
Question 4
If code is structured to handle the IOException before the FileNotFoundException, which of
these results is true?
Closeable
Catchable
Runnable
Serializable
Question 6
Which of the following statements is true about exception handling in Java:
A try block can have many catch blocks but only one finally block
A try block can have many catch blocks and many finally blocks
A try block must have one finally block for each catch block
A try block must have at least one catch block to have a finally block
Question 7
A JVM level problem that terminates the current runtime is a subtype of:
RuntimeException
Exception
FatalException
Error
Question 8
All unchecked exceptions extend which class at some point in their ancestry?
RuntimeException
Exception
FatalException
Error
Question 9
Which keyword raises an exception in Java code?
try
throw
break
throws
Question 10
Exceptions thrown in a try-with-resources block, after an exception has already been
thrown in the try block, are accessible as:
ResourceExceptions
HiddenException
CloseableExceptions
SuppressedExceptions
Java Threads
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution
running concurrently. This hands-on lab takes you through the basics of using Java threading.
Resources
Exercises
In this exercise, you are going to learn how to create and start a thread execution by writing a class that extends Thread class. You will
learn how to start the thread by either not having the start() method in the constructor of the subclass or having it in the constructor of
the subclass.
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in ExtendThreadClassTest0 as project name.
For Create Main Class field, type in ExtendThreadClassTest0. (Figure-1.10 below)
Click Finish.
Observe that ExtendThreadClassTest0 project appears and IDE generated ExtendThreadClassTest0.java is displayed in the
source editor window of NetBeans IDE.
2. Modify the IDE generated ExtendThreadClassTest0.java as shown in Code-1.11 below. Study the code by paying special attention
to the bold fonted parts. Note that the start() is invoked after the object instance of PrintNameThread class is created.
}
}
Code-1.11: ExtendThreadClassTest0.java
PrintNameThread(String name) {
super(name);
}
7. For your own exercise, modify ExtendThreadClassTest0.java as following. Build and run the application.
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in ExtendThreadClassTest2 as project name.
For Create Main Class field, type in ExtendThreadClassTest2.
Click Finish.
Observe that ExtendThreadClassTest2 project appears and IDE generated ExtendThreadClassTest2.java is displayed in the
source editor window of NetBeans IDE.
2. Modify the IDE generated ExtendThreadClassTest2.java as shown in Code-1.21 below.
PrintNameThread pnt1 =
new PrintNameThread("A");
PrintNameThread pnt2 =
new PrintNameThread("B");
PrintNameThread pnt3 =
new PrintNameThread("C");
}
}
Code-1.21: ExtendThreadClassTest2.java
3. Write PrintNameThread.java as shown in Code-1.22 below. Note that the start() method is invoked as part of the constructor
method of the PrintNameThread class.
PrintNameThread(String name) {
super(name);
// start() method is inside the constructor of the subclass
start();
}
AAAAAAAAAABBBBBBBBBBCCCCCCCCCC
Figure-1.23: Result of running ExtendThreadClassTest2 application
5. For your own exercise, modify ExtendThreadClassTest2.java as following. Build and run the application.
Summary
In this exercise, you have learned how to create and start a thread by extending Thread class.
In this exercise, you are going to create and start a thread by writing a class that implements Runnable interface.
1. Create and start a thread by implementing Runnable interface - start() method is not in the constructor
2. Create and start a thread by implementing Runnable interface - start() method is in the constructor
(2.1) Create and start a thread by implementing Runnable interface - start() method is not in the constructor
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in RunnableThreadTest1 as project name.
For Create Main Class field, type in RunnableThreadTest1.
Click Finish.
Observe that RunnableThreadTest1 project appears and IDE generated RunnableThreadTest1.java is displayed in the source
editor window of NetBeans IDE.
2. Modify the IDE generated RunnableThreadTest1.java as shown in Code-2.11 below. Study the code by paying special attention to
the bold fonted parts. Note that the start() method needs to be invoked explicitly after an object instance of the PrintNameRunnable
class is created.
}
}
Code-2.11: RunnableThreadTest1.java
String name;
PrintNameRunnable(String name) {
this.name = name;
}
ACBACBACBACBACABCABCABCABCABCB
Figure-2.13: Result of running RunnableThreadTest1 application
5. For your own exercise, do the following. Build and run the application.
(2.2) Create and start a thread by implementing Runnable interface - start() method is in the constructor
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in RunnableThreadTest2 as project name.
For Create Main Class field, type in RunnableThreadTest2.
Click Finish.
Observe that RunnableThreadTest2 project appears and IDE generated RunnableThreadTest2.java is displayed in the source
editor window of NetBeans IDE.
2. Modify the IDE generated RunnableThreadTest2.java as shown in Code-2.21 below. Study the code by paying special attention to
the bold fonted parts.
new PrintNameRunnable("B");
new PrintNameRunnable("C");
}
}
Code-2.21: RunnableThreadTest2.java
3. Write PrintNameRunnable.java as shown in Code-2.22 below. Study the code by paying special attention to the bold fonted parts.
Note that the start() method is in the constructor of the PrintNameRunnable class.
Thread thread;
PrintNameRunnable(String name) {
thread = new Thread(this, name);
thread.start();
}
ABCABCABCABCABCABCABCBACBACBAC
Figure-2.23: Result of running RunnableThreadTest2 application
Summary
In this exercise, you have learned how to create a class that implements Runnable interface and starts a thread.
In this exercise, you are going to learn how to display information on a ThreadGroup, how to set a thread priority, and so on.
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in ThreadGroupTest as project name.
For Create Main Class field, type in ThreadGroupTest.
Click Finish.
Observe that ThreadGroupTest project appears and IDE generated ThreadGroupTest.java is displayed in the source editor
window of NetBeans IDE.
2. Modify the IDE generated ThreadGroupTest.java as shown in Code-3.11 below. Study the code by paying special attention to the
bold fonted parts.
}
}
Code-3.11: ThreadGroupTest.java
5. For your own exercise, do the following. Build and run the application.
Modify ThreadGroupTest.java to create another (4th) SimpleThread instance using your capital city of your country.
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in DisplayAllThreads as project name.
For Create Main Class field, type in DisplayAllThreads.
Click Finish.
Observe that DisplayAllThreads project appears and IDE generated DisplayAllThreads.java is displayed in the source editor
window of NetBeans IDE.
2. Modify the IDE generated DisplayAllThreads.java as shown in Code-3.21 below. Study the code by paying special attention to the
bold fonted parts.
return list;
}
}
Code-3.21: DisplayAllThreads.java
5. For your own exercise, do the following. Build and run the application.
Modify DisplayAllThreads.java to create another (4th) SimpleThread instance using your capital city of your country.
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in ThreadsPriority as project name.
For Create Main Class field, type in ThreadsPriority.
Click Finish.
Observe that ThreadsPriority project appears and IDE generated ThreadsPriority.java is displayed in the source editor window
of NetBeans IDE.
2. Modify the IDE generated ThreadsPriority.java as shown in Code-3.31 below. Study the code by paying special attention to the
bold fonted parts.
}
}
Code-3.31: ThreadsPriority.java
0 Boston Priority = 10
0 Seoul Priority = 1
0 New York Priority = 5
1 Boston Priority = 10
1 Seoul Priority = 1
1 New York Priority = 5
2 Boston Priority = 10
2 Seoul Priority = 1
3 Boston Priority = 10
2 New York Priority = 5
4 Boston Priority = 10
3 New York Priority = 5
5 Boston Priority = 10
6 Boston Priority = 10
7 Boston Priority = 10
8 Boston Priority = 10
9 Boston Priority = 10
Done! Boston
4 New York Priority = 5
5 New York Priority = 5
6 New York Priority = 5
7 New York Priority = 5
8 New York Priority = 5
9 New York Priority = 5
Done! New York
3 Seoul Priority = 1
4 Seoul Priority = 1
5 Seoul Priority = 1
6 Seoul Priority = 1
7 Seoul Priority = 1
8 Seoul Priority = 1
9 Seoul Priority = 1
Done! Seoul
Figure-3.33: Result of running ThreadsPriority application
Summary
Exercise 4: Synchronization
In this exercise, you are going to exercise how to do synchronization among threads.
(4.1) Build and run a program in which threads are NOT synchronized
In this step, you are going to build an application that displays a result that is not desirable since threads are not synchronized.
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in UnsynchronizedExample as project name.
For Create Main Class field, type in UnsynchronizedExample.
Click Finish.
Observe that UnsynchronizedExample project appears and IDE generated UnsynchronizedExample.java is displayed in the
source editor window of NetBeans IDE.
}
Code-4.11: UnsynchronizedExample.java
Thread thread;
String str1, str2;
PrintStringsThread(String str1, String str2) {
this.str1 = str1;
this.str2 = str2;
thread = new Thread(this);
thread.start();
}
}
Code-4.12: PrintStringsThread.java
4. Write TwoStrings.java as shown in Code-4.13 below. Study the code by paying special attention to the bold fonted parts. Note that
the print method is not synchronized.
(4.2) Build and run a program in which threads are synchronized through synchronized method
In this step, you are going to build an application that displays a desired result because the threads are synchronized.
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in SynchronizedExample1 as project name.
For Create Main Class field, type in SynchronizedExample1.
Click Finish.
Observe that SynchronizedExample1 project appears and IDE generated SynchronizedExample1.java is displayed in the
source editor window of NetBeans IDE.
}
Code-4.21: SynchronizedExample1.java
Thread thread;
String str1, str2;
}
Code-4.22: PrintStringsThread.java
4. Write TwoStrings.java as shown in Code-4.23 below. Study the code by paying special attention to the bold fonted parts.
(4.3) Build and run a program in which threads are synchronized through synchronized statement on common object
In this step, you are going to build another application that displays a desired result because the threads are synchronized.
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in SynchronizedExample2 as project name.
For Create Main Class field, type in SynchronizedExample2.
Click Finish.
Observe that SynchronizedExample2 project appears and IDE generated SynchronizedExample2.java is displayed in the
source editor window of NetBeans IDE.
}
Code-4.31: SynchronizedExample2.java
3. Write PrintStringsThread.java as shown in Code-4.32 below. Study the code by paying special attention to the bold fonted parts.
Thread thread;
String str1, str2;
TwoStrings ts;
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in ProducerConsumerUnsynchronized as project name.
For Create Main Class field, type in ProducerConsumerUnsynchronized.
Click Finish.
p1.start();
c1.start();
}
}
Code-5.11: ProducerConsumerUnsynchronized.java
3. Write CubbyHole.java as shown in Code-5.12 below. Study the code by paying special attention to the bold fonted parts.
// Unsynchronized CubbyHole.
//
// Results are unpredictable; a number may be read before a number has
// been produced or multiple numbers may be produced with only one or
// two being read adding synchronization ensures that a number is first
// produced, then read in the correct order.
Producer #1 put: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Producer #1 put: 1
Producer #1 put: 2
Producer #1 put: 3
Producer #1 put: 4
Producer #1 put: 5
Producer #1 put: 6
Producer #1 put: 7
Producer #1 put: 8
Producer #1 put: 9
Figure-5.15: Result of running ProducerConsumerUnsynchronized application
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in ProducerConsumerSynchronized as project name.
For Create Main Class field, type in ProducerConsumerSynchronized.
Click Finish.
p1.start();
c1.start();
}
}
Code-5.21: ProducerConsumerSynchronized.java
3. Write CubbyHole.java as shown in Code-5.22 below. Study the code by paying special attention to the bold fonted parts.
Producer 1 put: 0
Consumer 1 got: 0
Producer 1 put: 1
Consumer 1 got: 1
Producer 1 put: 2
Consumer 1 got: 2
Producer 1 put: 3
Consumer 1 got: 3
Producer 1 put: 4
Consumer 1 got: 4
Producer 1 put: 5
Consumer 1 got: 5
Producer 1 put: 6
Consumer 1 got: 6
Producer 1 put: 7
Consumer 1 got: 7
Producer 1 put: 8
Consumer 1 got: 8
Producer 1 put: 9
Consumer 1 got: 9
Figure-5.25: Result of running ProducerConsumerSynchronized application
Summary
In this exercise, you have learned how to perform inter-thread commmunication by the usage of wait(), notify(), and notifyAll()
methods.
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects.
Click Next.
Under Name and Location pane, for the Project Name field, type in TimerReminder as project name.
For Create Main Class field, type in TimerReminder.
Click Finish.
Observe that TimerReminder project appears and IDE generated TimerReminder.java is displayed in the source editor window
of NetBeans IDE.
2. Modify the IDE generated TimerReminder.java as shown in Code-6.11 below. Study the code by paying special attention to the
bold fonted parts.
import java.util.Timer;
import java.util.TimerTask;
/**
* Simple demo that uses java.util.Timer to schedule a task to execute
* once 5 seconds have passed.
*/
Timer timer;
Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
Under Choose Project pane, select Java under Categories and Java Application under Projects. Click Next.
Under Name and Location pane, for the Project Name field, type in AnnoyingBeep as project name.
For Create Main Class field, type in AnnoyingBeep.
Click Finish.
Observe that AnnoyingBeep project appears and IDE generated AnnoyingBeep.java is displayed in the source editor window of
NetBeans IDE.
2. Modify the IDE generated AnnoyingBeep.java as shown in Code-6.21 below. Study the code by paying special attention to the bold
fonted parts.
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Toolkit;
/**
* Schedule a task that executes once every second.
* Beep every second.
*/
public AnnoyingBeep() {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1000); //subsequent rate
}
Summary
In this exercise, you have learned how to use Timer and TimerTask classes to schedule one-time or repeating tasks.
Homework