JAVA MULTITHREADING PROGRAMS
A thread is a line of execution it is the smallest unit of code. more than one thread work simultaneously execute one by one is called multithreading. more than one program work simultanously and execute one by one is called as multitasking.
METHOD OF THREADS
there are different method of threads:
- start()
- stop()
- run()
- wait()
- sleep()
- resume()
- suspend()
start() - it is used to start the thread that is a new thread.
stop() - it is used to stop the thread. it may be new thead or block thread.
run() - it is used to running the thread for compilation of life cycle.
wait() - it is used to wait the thread which is in a block.
sleep() -it is used to set the time of a thred.
resume() - it is used to resume the thread or restart the thread.
suspend() - it is used to suspend the thread in runnable process.
STATE OF A THREAD
there are four state of a thread:
- new thread
- runnable thread
- block thread
- dead thread
there are two way to create a thread:
- Thread class
- Runnable interface
EXAMPLES -
Q1)Define thread called “PrintTextThread” for printing text on command prompt for
„n‟ number of times. Create three threads and run them. pass the text and „n‟ as
parameters to the thread constructor. [30]
Example:
a) First thread prints “I am in FY” 10 times
b) Second thread prints “I am in SY” 20 times
c) Third thread prints “I am in TY” 30 times
--->
package slip11;
public class NThread extends Thread {
String msg;
int n;
public NThread() {
// TODO Auto-generated constructor stub
}
public NThread(int n,String a) {
// TODO Auto-generated constructor stub
this.n=n;
msg=a;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
NThread n1=new NThread(10, "I am in FY");
n1.start();
n1=new NThread(20, "I am in SY");
n1.start();
n1=new NThread(30, "I am in TY");
n1.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
int i=1;
while(i<=n){
System.out.println(i+msg);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
}
}
--------------------------------------------------------------------------------------
Q2)Define a thread to move numbers inside a panel vertically. The numbers should be
created between 0 – 9 when user clicks on the Start Button. Each number should
have a different color and vertical position (calculated randomly). Note: suppose
user has clicked Start button 5 times then five numbers say 0, 1, 5, 9, 3 should be
created and move inside the panel. Choice of number between 0 – 9 is random.
Ensure that number is moving within the panel border only.
--->
package slip5;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.*;
public class ZeroNineThread implements ActionListener{
JPanel jp;
JButton start,reset;
JFrame j;
MyThread t;
LinkedList li,l2;
boolean flag;
Random r;
public ZeroNineThread() {
// TODO Auto-generated constructor stub
j=new JFrame();
r=new Random();
j.setLayout(null);
j.setBounds(0, 0, 1200, 800);
jp=new JPanel();
jp.setLayout(null);
jp.setBackground(Color.black);
jp.setBounds(50, 00, 1100, 600);
j.add(jp);
start=new JButton("start");
start.setBounds(100, 630, 80, 20);
start.addActionListener(this);
j.add(start);
reset=new JButton("reset");
reset.setBounds(250, 630, 80, 20);
reset.addActionListener(this);
j.add(reset);
j.setVisible(true);
l2=li=new LinkedList();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ZeroNineThread();
}
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
int val;
if(ae.getActionCommand().equals("start")){
val=r.nextInt(10);
if(!li.contains(val+"")){
li.add(val+"");
flag=true;
t=new MyThread(val);
t.start();
}
}else{
flag=false;
li=new LinkedList<>();
}
}
class MyThread extends Thread{
JLabel jp1;
int v1=0,h;
boolean f=true;
public MyThread(int j) {
// TODO Auto-generated constructor stub
h=j;
jp1=new JLabel(j+"");
jp1.setForeground(new Color(r.nextInt(100),r.nextInt(100)+r.nextInt(100), r.nextInt(100)));
jp.add(jp1);
//System.out.println("hor:"+h);
}
@Override
public void run() {
// TODO Auto-generated method stub
while(flag){
jp1.setBounds(h*100, v1, 30, 20);
if(f)v1+=20;
else v1-=20;
try {
Thread.sleep(800);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(v1==580) f=false;
if(v1==0)f=true;
}
jp1.setForeground(Color.black);
jp1.setVisible(false);
}
}
}
Q1)Define thread called “PrintTextThread” for printing text on command prompt for
„n‟ number of times. Create three threads and run them. pass the text and „n‟ as
parameters to the thread constructor. [30]
Example:
a) First thread prints “I am in FY” 10 times
b) Second thread prints “I am in SY” 20 times
c) Third thread prints “I am in TY” 30 times
--->
package slip11;
public class NThread extends Thread {
String msg;
int n;
public NThread() {
// TODO Auto-generated constructor stub
}
public NThread(int n,String a) {
// TODO Auto-generated constructor stub
this.n=n;
msg=a;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
NThread n1=new NThread(10, "I am in FY");
n1.start();
n1=new NThread(20, "I am in SY");
n1.start();
n1=new NThread(30, "I am in TY");
n1.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
int i=1;
while(i<=n){
System.out.println(i+msg);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
}
}
--------------------------------------------------------------------------------------
Q2)Define a thread to move numbers inside a panel vertically. The numbers should be
created between 0 – 9 when user clicks on the Start Button. Each number should
have a different color and vertical position (calculated randomly). Note: suppose
user has clicked Start button 5 times then five numbers say 0, 1, 5, 9, 3 should be
created and move inside the panel. Choice of number between 0 – 9 is random.
Ensure that number is moving within the panel border only.
--->
package slip5;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.*;
public class ZeroNineThread implements ActionListener{
JPanel jp;
JButton start,reset;
JFrame j;
MyThread t;
LinkedList li,l2;
boolean flag;
Random r;
public ZeroNineThread() {
// TODO Auto-generated constructor stub
j=new JFrame();
r=new Random();
j.setLayout(null);
j.setBounds(0, 0, 1200, 800);
jp=new JPanel();
jp.setLayout(null);
jp.setBackground(Color.black);
jp.setBounds(50, 00, 1100, 600);
j.add(jp);
start=new JButton("start");
start.setBounds(100, 630, 80, 20);
start.addActionListener(this);
j.add(start);
reset=new JButton("reset");
reset.setBounds(250, 630, 80, 20);
reset.addActionListener(this);
j.add(reset);
j.setVisible(true);
l2=li=new LinkedList();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ZeroNineThread();
}
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
int val;
if(ae.getActionCommand().equals("start")){
val=r.nextInt(10);
if(!li.contains(val+"")){
li.add(val+"");
flag=true;
t=new MyThread(val);
t.start();
}
}else{
flag=false;
li=new LinkedList<>();
}
}
class MyThread extends Thread{
JLabel jp1;
int v1=0,h;
boolean f=true;
public MyThread(int j) {
// TODO Auto-generated constructor stub
h=j;
jp1=new JLabel(j+"");
jp1.setForeground(new Color(r.nextInt(100),r.nextInt(100)+r.nextInt(100), r.nextInt(100)));
jp.add(jp1);
//System.out.println("hor:"+h);
}
@Override
public void run() {
// TODO Auto-generated method stub
while(flag){
jp1.setBounds(h*100, v1, 30, 20);
if(f)v1+=20;
else v1-=20;
try {
Thread.sleep(800);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(v1==580) f=false;
if(v1==0)f=true;
}
jp1.setForeground(Color.black);
jp1.setVisible(false);
}
}
}
--------------------------------------------------------------------------------------------
Q3)Write a program to calculate the sum and average of an array of 1000 integers
(generated randomly) using 10 threads. Each thread calculates the sum of 100
integers. Use these values to calculate average. [Use join method].
--->
package slip12;
import java.util.*;
public class CalculateThread extends Thread {
int array[];
int sum=0;
public int getSum() {
return sum;
}
public CalculateThread() {
// TODO Auto-generated constructor stub
Random r=new Random();
array=new int[100];
for(int i=0;i<array.length;i++)array[i]=r.nextInt(1000);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CalculateThread t[]=new CalculateThread[10];
int sum=0;
for(int i=0;i<t.length;i++){
t[i]=new CalculateThread();
t[i].start();
}
for(int i=0;i<t.length;i++){
sum+=t[i].getSum();
}
System.out.println("Sum of 1000 integer is:\t"+sum);
float f=sum/100;
System.out.println("Avarage of 1000 integer is:\t"+f);
}
@Override
public void run() {
// TODO Auto-generated method stub
int i=0;
while(i<array.length){
sum+=array[i];
i++;
}
}
}
--------------------------------------------------------------------------------------------------
Q4)Define a thread to move alphabets inside a panel vertically. The alphabets should be
created between A – Z when user clicks on the Start Button. Each alphabets should
have a different color and vertical position (calculated randomly). Note: Suppose
user has clicked buttons 5 times then five alphabets say A, E, C, G, J should be
created and move inside the panel. Choice of alphabets between A-Z is random.
Ensure that alphabet moving within the panel border only.
--->
package slip18;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.*;
public class AZThread implements ActionListener{
JPanel jp;
JButton start,reset;
JFrame j;
MyThread t;
LinkedList<Integer> li;
boolean flag;
Random r;
public AZThread() {
// TODO Auto-generated constructor stub
j=new JFrame();
r=new Random();
j.setLayout(null);
j.setBounds(0, 0, 1200, 800);
jp=new JPanel();
jp.setLayout(null);
jp.setBackground(Color.black);
jp.setBounds(50, 00, 1100, 600);
j.add(jp);
start=new JButton("start");
start.setBounds(100, 630, 80, 20);
start.addActionListener(this);
j.add(start);
reset=new JButton("reset");
reset.setBounds(250, 630, 80, 20);
reset.addActionListener(this);
j.add(reset);
j.setVisible(true);
li=new LinkedList<Integer>();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new AZThread();
}
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
int val;
if(ae.getActionCommand().equals("start")){
val=r.nextInt(91-65);
if(!li.contains(val)){
li.add(val);
flag=true;
t=new MyThread(val);
t.start();
}
}else{
flag=false;
li=new LinkedList<Integer>();
}
}
class MyThread extends Thread{
JLabel jp1;
int v1=0,h;
boolean f=true;
public MyThread(int j) {
// TODO Auto-generated constructor stub
h=j;
jp1=new JLabel((char)(j+65)+"");
jp1.setForeground(new Color(r.nextInt(100),r.nextInt(100)+r.nextInt(255-140), r.nextInt(100)));
jp.add(jp1);
//System.out.println("hor:"+h);
}
@Override
public void run() {
// TODO Auto-generated method stub
while(flag){
jp1.setBounds(h*30, v1, 30, 20);
if(f)v1+=20;
else v1-=20;
try {
Thread.sleep(800);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(v1==580) f=false;
if(v1==0)f=true;
}
jp1.setForeground(Color.black);
jp1.setVisible(false);
}
}
}
----------------------------------------------------------------------------------------------
0 Comments