OOP JAVA M4 Ktunotes - in
OOP JAVA M4 Ktunotes - in
Module 4
Main Thread
start
start start
Thread Thread
A Thread B
C
Client 2 Process
Internet
Server
Local Area Network
PDA
Downloaded from Ktunotes.in
7
Multithreaded programming
● Java is a multithreaded programming language
● A thread is an independent path of execution within a program, most of the
programs are single threaded
● Multithreading refers to two or more tasks executing concurrently within a single
program.
● Many threads can run concurrently within a program.
● Every thread in Java is created and controlled by the java.lang.Thread class.
● A Java program can have many threads, and these threads can run concurrently,
either asynchronously or synchronously.
• Blocked for I/O: The Thread waits for a blocking operation to complete.
• Blocked for join completion: The Thread waits for completion of another Thread.
• Waiting for notification: The Thread waits for notification another Thread.
• Blocked for lock acquisition: The Thread waits to acquire the lock of an object.
MyThread MyClass
Create a new class that extends Thread class using the following steps.
● Step 1 - You will need to override run( ) method available in Thread class.
● This method provides an entry point for the thread
● Syntax of run() method − public void run( )
● Step 2 - Once Thread object is created, you can start it by calling start()
method, which executes a call to run( ) method.
● Syntax of start() method − void start( );
• Thread()
• Thread(String name)
• Thread(Runnable r)
Step 2 - As a second step, you will instantiate a Thread object using the following
constructor − Thread(Runnable threadObj, String threadName);
Step 3 - Once a Thread object is created, you can start it by calling start() method,
which executes a call to run( ) method. [ void start(); ]
class ThreadEx1 {
public static void main(String [] args ) {
MyThread t = new MyThread();
t.start();
}
}
Downloaded from Ktunotes.in
31
2nd method: Threads by
implementing Runnable interface
■ Create a class that implements the interface Runnable and
override run() method:
class MyThread implements Runnable
{
.....
public void run()
{
// thread body of execution
}
}
■ Creating Object:
MyThread myObject = new MyThread();
■ Creating Thread Object:
Thread thr1 = new Thread( myObject );
■ Start Execution:
thr1.start();
Downloaded from Ktunotes.in
32
An example
class MyThread implements Runnable {
public void run() {
System.out.println(" this thread is running ... ");
}
}
class ThreadEx2 {
public static void main(String [] args ) {
Thread t = new Thread(new MyThread());
t.start();
}
}
Downloaded from Ktunotes.in
33
Creating Multiple Threads
● This can be done by creating the instances of the classes which are either
extending Thread class or implementing runnable interface.
● TODO: Run a simple program which creates multiple threads and identify the
maximum number of threads possible from your program.
● Join method can join the newly created thread with main thread
Syntax
public final void suspend()
The second way for threads to communicate is by using thread control methods.
● suspend ( ): A thread can suspend itself and wait till another thread resume it.
● resume ( ): A thread can wake up other waiting thread
● join ( ) :This method can be used for the caller thread to wait for the completion of called thread.
The third way for threads to communicate is the use of three methods; wait(), notify(), and notifyAll();
these are defined in class Object of package java.lang.
● wait ( ): tells the calling thread to give up the monitor and make the calling thread wait until either
a time out occurs or another thread call the same thread's notify() or notifyAll() method.
● notify ( ): will wake up the only one (first) waiting thread called wait() on the same object.
● notifyAll( ): will wake up all the threads that have been called wait( ) on the same object.
char[ ] ch={‘h','a',‘i',‘j',‘a',‘v',‘a'};
is same as:
String s = “haijava";
• Java String class provides a lot of methods to perform operations on strings such as compare(),
concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.
For Example:
String s = "welcome";
• Each time you create a string literal, the JVM checks the "string constant pool" first.
• If the string already exists in the pool, a reference to the pooled instance is returned.
• If the string doesn't exist in the pool, a new string instance is created and placed in the pool.
For example:
String s1="Hello";
String s2="Hello"; //It doesn't create a new instance
• Internal implementation
public int length() {
return value.length;
}
Signature - The signature of the string length() method is given
below:
public int length()
By equals() method
By = = operator
By compareTo() method
Signature
• The signature of string contains() method is given below:
public boolean contains(CharSequence sequence)
Internal implementation
public boolean contains(CharSequence s)
{
return indexOf(s.toString()) > -1;
}
Internal implementation
public String replaceAll(String regex, String replacement)
{
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
Signature
public String replaceAll(String regex, String replacement)
Internal implementation
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
The StringBuffer class in java is same as String class except it is mutable i.e. it can be
changed.
● Java Collections can achieve all the operations that you perform on a data such as
searching, sorting, insertion, manipulation, and deletion.
● Java Collection framework provides many interfaces (Set, List, Queue, Deque) and
classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet,
TreeSet).
Downloaded from Ktunotes.in
Interfaces & Classes in Collection Framework
– NoSuchElementException
– IllegalStateException
• Exceptions in the Methods Defined by ListIterator
– NoSuchElementException
– IllegalStateException
– UnsupportedOperationException
• But for loop can only be used to cycle through a collection in the forward direction, and we
can’t modify the contents of the collection.
• But for loop can only be used to cycle through a collection in the forward direction, and we
can’t modify the contents of the collection.
Background Events - Those events that require the interaction of end user are
known as background events.
Operating system interrupts, hardware or software failure, timer expires, an
operation completion are the example of background events.
Container getContainer( )
• The getChild( ) method returns a reference to the component that was added to or
removed from the container.
Component getChild( )
● GeeksforGeeks - www.geeksforgeeks.org
● Wikipedia - www.wikipedia.org
● Tutorialspoint - www.tutorialspoint.com
● https://itsmeebin.wordpress.com/
● https://renethajb.wordpress.com/
Disclaimer - This document contains images/texts from various internet sources. Copyright belongs to the
respective content creators. Document is compiled exclusively for study purpose and shall not be used for
commercial purpose.
Downloaded from Ktunotes.in