Multithreading and Event Handling in JAVA
Multitasking:-
- Multiple tasks are performed at a time is called as multitasking.
- We can perform a multiple tasks with the help of thread or a process.
Multithreading:-
- It is a type of multitasking in which multiple tasks are performed with the help of thread.
Multiprocessing:-
- Multiprocessing is a type of multitasking in which multiple tasks are performed with the help of multiple processes.
Multiple Programming:-
- If the multiple tasks are performed on a multiple processor then it is called as multiple programming.
Multicore Programming:-
- If the multiple tasks are performed with the help of different parts of same processor (core) then it is called as multiple Programming.
Multiuser:-
- If multiuser shares a copy of an operating system then it is called as a multiuser operating system.
Thread Synchronization:-
e.g.
public class demo
{
private int balance;
public synchronized debit(int amt)
{
while(bal_amt<0)
wait();
bal = amt;
}
public synchronized credit(int amt)
{
bal = bal-amt;
notify();
}
}
Thread class constructors:-
- Thread();
- Thread(String tname);
- Thread(Runnable object);
- Thread(Runnable object, String s);
- Thread(ThreadGroup t, String s);
- Thread(ThreadGroup t, Runnable object);
- Thread(ThreadGroup t, Runnable object, String s);
Inter thread communication:-
e.g.
class demo extends Thread
{
String str;
public demo(String s)
{
str = s;
}
public void run()
{
try{
for(;;)
{
System.out.println(str);
sleep(1000);
}
}
catch(InterruptException e)
{
System.out.println(e);
}
}
public ststic void main(String args[])
{
Thread t1 = new Thraed(first);
t1.start();
Thread t2 = new Thread(second);
t2.start();
}
}
class hello implements Runnable
{
String str;
public hello(String s)
{
str = s;
}
public void run()
{
try{
for(;;)
{
System.out.println(str);
sleep(1000);
}
}
catch(Exception e){…}
}
}
Multithreading:
- Thread means single block or a function which run concurrently.
- If there are multiple functions or a block in our application then that application is called as multithreaded application.
Important points about a Thread:-
- Thread has its own local variable.
- Threads share all the open files which are opened by its parent process.
- Thread has its own stack.
- Thread has its own CPU quantum.
- Any thread can kill any other thread.
- An operating system can kill any thread at any time without intimation but in case of java application role of an operating system is performed by JVM.
- Thread can communicate with another thread by using communication methods
a) notify()
b) notifyAll()
c) wait()
- Each thread has its own priority.
- When our thread gets scheduled, it is depend on the operating system strategies.
Different states of a Thread:-
Born state:-
- Sin this state, thread is only created but its execution is not started.
Ready state:-
- In this state, thread is created as well as it is ready to run but there is no CPU quantum is allocated for that state.
Running state:-
- Thread is created and for that thread actual CPU quantum is allocated and that thread starts its life cycle for this point.
Dead state:-
- At this state, thread executes its own function.
- This dead state is achieved successfully or not successfully.
- After the dead state, our thread cannot run again but in case of ready and running state thread can scheduled again and again.
e.g.
catch(InterruptException e)
{
System.out.println(e);
}
public static void main(String args[])
{
Runnable t1 = new hello(“first”);
new Threaf(t1).start();
Runnable t2 = new hello(“second”);
new Thread(t2).start();
}
Methods of Thread class:-
- start()
- isAlive()
- run()
- yield()
- stop()
- join()
- sleep()
Thread information:-
- setName()
- getName()
- dumpStack()
- getThread()
Thread priorities:-
- MIN_PRIORITY(1)
- NORM_PRIORITY
- getPriority()
- setPriority()
- MAX_PRIORITY(10)
Event handling:-
- When we click on any device or when we interact with any device first signal is generated and that signal is accepted by the device driver of that device.
- After accepting this signal device driver forward this signal to the operating system.
- An operating system accepts that signal or an event and converts that event into a message.
- Message is descriptive format of that event.
- After creating the message an operating system forward that message to the currently running application.
- To handle that event application has to provide a method which is used for handling that event.
- Sending a message to our application means calling a method indirectly.
- The method is used for accepting an event is called as event or an action listener method.
- After invoking an actionListener method that method provides a specific action for that event.
- The above model is called as delegation in java and in windows it is called as an event driven architecture.
- When we write our own actionListener that actionListener must be registered towards an operating system or JVM.
- This step of registering the actionListener is important because this actinListener method is called by the operating system directly.
- To register the actionListener we can use addActionListener method.
- After registering this method our operating system or JVM call this method when event is occurred.
- This method is called as callback method in terms of operating system.
- Above model is called as events and delegates in .NET.
Hardware/Device (signal to) ===>Device Driver (event) ===>Operating system (message) ===>JVM (event) ==>Application===>actionListener===>Approprite action is taken
For more reading about technology news in singapore and seo to online marketing do view more about other pages.