Multitasking:
- Executing several tasks simultaneously is the concept of Multitasking.
- The main objective of multitasking is to decrease response time of the system, so that the performance of the system will increase.
- Executing several tasks simultaneously where each task is a independent program.
- Example typing a java program in the Editor, playing MP3 player, Downloading a file from the internet
- All the tasks are independent of each other and executes simultaneously.
- This type of multitasking is useful at OS level.
- CGI fallows process based multitasking.
Thread Based Multitasking:-
- Executing several tasks simultaneously, where each task is a separate inependent part of the same program. That independent part is called the Thread.
- Servlets fallowThread Based MultiTasking.
- Java itself provide support for multithreading by introducing several library classes.
- We can use in games/animation programme.
- Creating a thread in two ways.
- By extending Thread
- By implementing Thread
- Methods used to prevent a thread from execution
- Synchronization
- InterThread communication (wait, notify, notifyAll)
Defining, Instantiating and starting Thread by extending Thread class:
class MyThread extends Thread
{
public void run()
{
System.out.println(“My job”);
}
}
- Here run method is the heart of thread where you have to define the job.
{
public static void main(String a[])
{
MyThread t=new MyThread();
t.start(); //starting a thread
System.out.println(“Main thread job”);
}
}
Ex:
class MyThread extends Thread
{
for(int i=0;i<10;i++) t="new" i="0;i<10;i++)" style="font-weight: bold;">1. Difference between t.start() & t.run()?
• If we call t.start(), it will create a new Thread and that thread is responsible for execution of run().
• If we call t.run(), mo new thread will create, the main thread only will execute run(), just like a normal method.
Thread class start():-
public void start()
{
1. Registering our thread with the thread scheduler then only thread scheduler allocate CPU and memory type of resources for this new thread also.
2. calls run()
}
public void start()
{
System.out.println(“start method”);// start method 1 time, main thread 10 times
}
public void start()
{
super.start();
System.out.println(“start method”);// start method child & main thread alternatively
}
class MyThread extends Thread
{
public static void main(String a[])
{
MyThread t=new MyThread();
t.start();
}
}
• Thread class run() has empty implementation that’s why we are getting no o/p.
Thread life cycle

{
public void run()
{
System.out.println(“run”);
}
}
class ThreadDemo
{
public static void main(String a[])
{
MyThread t= new MyThread();
Thread t1=new Thread(t);
t1.start();
// t1.run(); ----> It is just like a method no new thread will create
}
}
Ex:
class MyRunnable implemts Runnable
{
public void run()
{
for(int i=0;i<10;i++) r="new" t="new"> Target Runnable
for(int i=0;i<10;i++) style="font-weight: bold;">Thread class Constructors:
1. Thread();
2. Thread(Runnable r);
3. Thread(String name);
4. Thread(Runnable r, String name);
5. Thread(ThreadGroup g, String name);
6. Thread(ThreadGroup g, Runnable r);
7. Thread(ThreadGroup g, Runnable r, String name);
Ex:- // Test king
class MyThread extends Thread
{
public void run()
{
System.out.println(“run”);
}
}
class ThreadDemo
{
public static void main(String a[])
{
MyThread t= new MyThread();
Thread t1=new Thread(t);
t1.start();
// t1.run(); ----> It is just like a method no new thread will create
}
}
Setting & getting the name of a thread:
Thread class contain the fallowing methods for setting & getting the name of the Threads..
- public final String getName();
- public final void setName(String s);
• Thread class contains the fallowing methods for setting and getting priority of a thread.
- public final int getPriority();
- public final void setPriority(int i);
• Thread class contains the fallowing predefined priority constraints.
MAX-PRIORITY ---> 10
NORM-PRIORITY --->5 (default)
MIN-PRIORITY ---->10
• If you are trying to set the priority as greater than 10 or lessthan1, then we will get a RTE saying “Illegal Argument Exception”
class MyThread extends Thread
{}
class Sample
{
public static void main(String a[])
{
Thread.currentThread().setPriority(10);
System.out.println(Thread.currentThread().getPriority());
MyThread t= new MyThread();
System.out.println(t.getPriority());//10 10
}
}
• The default priority for the maintained is 5. But we are allowed to change the prority by using setPriority()
• The priority of any the thread is inherited from the parent thread.
• Thread scheduler uses these proroites to allocate CPU. The thread which is having highest priority will get executes first.
t.setPriority(10);
Yield() :
• We can prevent a thread from execution by using one of the fallowing methods.
1. yield()
2. join()
3. sleep()
• The thread which is executing yield() causes the current thread temperarly pause and allow other waiting threads of same or high priority to execute.
• If there is no waiting thread, the same thread will execute immediately.
• If all the remaining threads are having low priority, then the same thread once again will execute.
Method Signature:-
public static native yield();
MyThread t= new MyThread
• If the thread calls yield(), the thread going to ready state.
Ex:
Class MyThread extends Thread
{
public void run()
{
for(int i=0;i<10;i++) t =" new" i="0;i<10;i++);" style="font-weight: bold;">Join():
public final void join() throws InterruptedException
public final void join(long ms)throws InterruptedException
public final void join(long ms, int nanosec)throws InterruptedException
MyThread t= new MyThread
Example:
class MyThread extends Thread
{
public void run()
{
for(int i=0;i<10;i++) t="new" i="0;i<10;i++);" style="font-weight: bold;">sleep():-
• The thread which is executing sleep() will go to the sleep for the specified amount of time.
MethodSignature:
public static void sleep(long ms) throws InterruptedException
public static void sleep(long ms, int nanoseconds)throws InterruptedException
MyThread t= new MyThread
Ex:
class MyThread extends Thread
{
public void run()
{
for(int i=0;i<10;i++) t="new" style="font-weight: bold;">Ex: class MyThread extends Thread
{
public void run()
{
for(int i=0;i<10;i++) t="new" style="font-weight: bold;">Synchronization:
- The keyword synchronized can be apply only for methods and blocks. Ie., We can’t apply synchronized keyword for the classes and variables.
- If a method (or block) declared as the synchronized at a time only one thread is allowed to execute that synchronized area on any given object.
- Advantage: Prevent data corruption, achieve security.
- Limitation: Because of synchronized keyword 2 threads are not allowed to execute concurrently on any object, hence the waiting time of the threads will increase, which results in low performance of the system.
- A class can contain both synchronized and non synchronized methods.
- If a thread calls synchronized method on any object, first this thread got the lock, it is allowed to any synchronized method on that object.
- If a thread executing any synchronized method on the given object at that time no other thread is allowed to execute any synchronized method on that object.
- If a thread executing any synchronized method the remaining threads are allowed simultaneously to execute nay non synchronized method on that object.
- Every Object in java has a lock. At the time of synchronization only the lock concept will conme into the picture.
Ex:-
class Display
{
public void show(String name)
{
for(int i=0;i<10;i++) d="d;" name="name;" d="new" t1="new" t2="new" style="font-weight: bold;">Class Level lock:
- If you want to execute any static synchronized method, first the thread should required a class level lock.
- If a thread has class level lock, it is allowed to execute any stock synchronized method. During that time no other thread is allowed to execute any static synchronized method. But the remaining threads are allowed to execute any static non synchronized methods of the same class.
Beware of making too many assumptions about how thread priorities and/or yield() actually do. It's highly system dependent and may not be what you expect!
ReplyDeleteSee for example: http://www.javamex.com/tutorials/threads/priority.shtml