Professional Documents
Culture Documents
WHAT IS MULTITHREADING?
It doesn't block the user because threads are independent and you can
perform multiple operations at the same time.
You can perform many operations together, so it saves time.
Threads are independent, so it doesn't affect other threads if an exception
occurs in a single thread.
MULTITASKING
36) static void setDefaultUncaug It sets the default handler invoked when a thread abruptly
htExceptionHandl terminates due to an uncaught exception.
er()
CREATING A THREAD
Implement Syntax
class SecondThread implements Runnable {
public void run()
{
System.out.println("Run method executed by
child Thread");
}
}
RUNNING THREADS
If the class extends the Thread class, the thread can be run by creating an instance of the class and call its
start() method:
Extend Example
class FirstThread extends Thread
{
public void run()
{
System.out.println("Run method executed by child Thread");
}
public static void main(String[] args)
{
FirstThread t = new FirstThread();
t.start();
System.out.println("Main method executed by main thread");
}
}
RUNNING THREADS
If the class implements the Runnable interface, the thread can be run by passing an instance of the class to
a Thread object's constructor and then calling the thread's start() method:
Implement Example
public class MyClass implements Runnable {
public static void main(String[] args) {
MyClass obj = new MyClass();
Thread thread = new Thread(obj);
thread.start();
System.out.println("This code is outside of the thread");
}
public void run() {
System.out.println("This code is running in a thread");
}
}
DIFFERENCES BETWEEN "EXTENDING" AND
"IMPLEMENTING" THREADS
Because threads run at the same time as other public class ConcurrentProblem extends Thread{
public static int amount =0;
parts of the program, there is no way to know public void run(){
in which order the code will run. When the amount++;
threads and main program are reading and }
writing the same variables, the values are
unpredictable. The problems that result from public static void main(String arg[])
{
this are called concurrency problems. ConcurrentProblem CP=new ConcurrentProblem();
Example CP.start();
System.out.println(amount);
A code example where the value of the amount++;
variable amount is unpredictable: System.out.println(amount);
}
}
public class ConcurrentProblem extends Thread{
CONCURRENCY PROBLEMS public static int amount =0;
public void run(){
amount++;
}
To avoid concurrency problems, it is best to public static void main(String arg[])
{
share as few attributes between threads as ConcurrentProblem CP=new ConcurrentProblem();
possible. If attributes need to be shared, one CP.start();
possible solution is to use the isAlive()
//wait for thread to finish
method of the thread to check whether the
thread has finished running before using any while(CP.isAlive()) {
attributes that the thread can change. System.out.println("Waiting...");
}
Example
System.out.println("Main" + amount);
Use isAlive() to prevent concurrency amount++;
problems: System.out.println("Main"+amount);
}
}
SLEEP METHOD IN JAVA
The sleep() method of Thread class is used to sleep a thread for the specified
amount of time.
Syntax of sleep() method in java
The Thread class provides two methods for sleeping a thread:
t1.start();
t2.start();
}
}
CAN WE START A THREAD TWICE
t1.start();
t2.start();
t1.setName("Java Program");
System.out.println("After changing name of
t1:"+t1.getName());
}
}
The currentThread() method:
The currentThread() method returns a reference to the currently executing thread object.
System.out.println(Thread.currentThread().getName());
}
public static void main(String args[])
{ run:
TestJoinMethod4 t1=new TestJoinMethod4(); Thread-0
TestJoinMethod4 t2=new TestJoinMethod4(); Thread-1
BUILD SUCCESSFUL (total time: 0
t1.start(); seconds)
t2.start();
}
}
To be Continued…