Let Us Know About Multithreading In Java


This resource contains the information about multithreading in java etc.

Let Us Know About Multithreading In Java



Thread class, Runable interface are used to do Multithreading. Thread class contains predefined method start(). It runs run() method. run() is overridable method of Thread class. Here all threads run methods are operated in shared mode.


Thread2.javaMultithreading_ThreadLifeCycle
yield() method gives execution chance to next thread. stop() method kills thread. sleep() method blocks thread for given time period. After completing that time period, that thread continues automatically.
1000 million seconds mean 1 second. Sleeping thread may raise runtime exception called InterruptedException. It is checked exception. Due to this, writing try-catch blocks or throws list is compulsory.

Thread1.java
Priority means preference. Every thread contains priority. Threads priority range is from 1 to 10. 1 is minimum priority, 10 is maximum priority. 5 is normal priority.
Thread class contains static constants for priority values.
MIN_PRIORITY 1
NORM_PRIORITY 5
MAX_PRIORITY 10
Thread class contains methods to retrieve, set priority values.
getPriority() method is used to retrieve existing priority of thread. setPriority() method is used to set new priority value to thread.
When lower priority thread is executing, if higher priority thread is started, lower priority thread waits for the completion of higher priority thread execution.

DemoJoin.java
isAlive() method of Thread class returns true when thread is in living mode, otherwise returns false. join() method of Thread class joins child thread to parent thread.
Then parent thread waits for the completion of joined child thread.
Java does not support multiple inheritances with classes. To overcome this problem, interfaces are introduced.
Runnable interface is used where Thread class is not able to write. Runnable interface contains run() method which is overridable.
Syncronization is the method of preventing sharing. synchronized() method is used to prevent sharing.

wait() method blocks thread.
notify() method continues the thread which is currently blocked by wait() method.
notifyAll() method continues all threads which are currently blocked.
These methods belong with Thread class.
wait() is replacement of suspend() method.
notify() is replacement of resume() method.

Package
Package is container for classes, interfaces and sub packages.
java.lang.Thread, java.lang.Runnable Here java package contains lang package. Thread class, Runnable interface are in lang package.
Interface: java does not support multiple inheritances with classes. Due to this, interfaces are introduced. Interfaces support multiple inheritances. It contains final type data members and declaration of methods. If data member is final, its value is not changed. If member function is final, is not overridden. If class is final, it is not inherited.

Example:
Multithreading odd or even numbers

import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;

class test extends Frame implements ActionListener,Runnable
{
int lower,upper;
Label l1=new Label("ODD");
Label l2=new Label("EVEN");
List lb1=new List();
List lb2=new List();
Button b2=new Button("EXIT");
test(int low,int up)
{
lower = low;
upper = up;
setLayout(new FlowLayout());
setSize(700,700);
setTitle("Thread Demo");
setVisible(true);
add(l1);add(lb1);add(l2);add(lb2);add(b2);
b2.addActionListener(this);
Thread t=new Thread(this);
t.start();


addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0); }
}
);

}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b2)
System.exit(0);
}
public void run()
{
try
{
if((lower % 2) != 0)
{
lower = lower + 1;
}
while(lower <= upper)
{
Thread.sleep(1000);
lb2.add(String.valueOf(lower));
lower += 2;
Thread.sleep(500);
}
}catch(Exception e){}
}

public static void main(String args[])
{
int lower,upper;
lower = Integer.parseInt(args[0]);
upper = Integer.parseInt(args[1]);
test ob = new test(lower,upper);

if((lower % 2) == 0)
{
lower = lower + 1;
}

try
{
while(lower <= upper)
{
Thread.sleep(1000);
ob.lb1.add(String.valueOf(lower));
lower = lower + 2;
Thread.sleep(500);
}
}
catch(Exception e){}
}

}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: