0% found this document useful (0 votes)
48 views

Java 11

The document discusses multithreading concepts in Java through three code examples. The first example creates two thread objects that print messages when started. The second example splits an array summation across 10 thread objects. The third example generates random numbers across 10 thread objects and prints the sums and averages.

Uploaded by

Purva Jain
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Java 11

The document discusses multithreading concepts in Java through three code examples. The first example creates two thread objects that print messages when started. The second example splits an array summation across 10 thread objects. The third example generates random numbers across 10 thread objects and prints the sums and averages.

Uploaded by

Purva Jain
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

name:kajol wadekar.

rno:54201

exercise 11:Multithreading..

==============================================================================

set A:

1)

class RunnableThread extends Thread


{
String msg;
public RunnableThread()
{
}

public RunnableThread(String message)


{
this.msg=message;
}
public void run()
{
try
{
System.out.println(this.msg);
}
catch(Exception ie)
{
}
}
}

public class e8a1


{
public static void main(String args[])
{
RunnableThread thread1=new RunnableThread("hi...");

RunnableThread thread2=new RunnableThread("how are you...?");

System.out.println(thread1);
System.out.println(thread2);
thread1.start();
thread2.start();
}
}

-----------------------------------------------------------------------------------
--------

2)

class thread extends Thread


{
int pos=0;
int ar[];
int sum=0;
thread(int p,int arr[])
{
pos=p;
ar=arr;
}

public void run()


{
try
{
for(int i=pos;i<pos+100;i++)
{
sum=sum+ar[i];
}
}
catch(Exception e)
{
}
}

int getsum()
{
return sum;
}
}
class e8a2
{
public static void main(String args[])throws InterruptedException
{
int j=0,sum=0;
int arr[]=new int[1000];
thread t[]=new thread[10];

for(int i=0;i<1000;i++)

arr[i]=i+1;

for(int i=0;i<10;i++)
{
t[i]=new thread(j,arr);
t[i].start();
t[i].join();
j=j+100;
}

for(int i=0;i<10;i++)
System.out.println(t[i]);
for(int i=0;i<10;i++)
{
System.out.println("sum of"+t[i]+"=="+t[i].getsum());
System.out.println("Avg="+(float)t[i].getsum()/1000);
sum=sum+t[i].getsum();
}

System.out.println("sum of 100 no is"+sum);


}
}
-----------------------------------------------------------------------------------
------------

3)

import java.util.*;

class thread1 implements Runnable


{
Thread t;
int i,no,sum;
int a[]=new int[1000];

thread1(String s,int n)
{
Random rs=new Random();
t=new Thread(this,s);
no=n;
int j=0;
for(i=1;i<=1000;i++)
{
a[i]=rs.nextInt()%100;
j++;
}
t.start();
}

public void run()


{
for(i=0;i<100;i++)
{
sum=sum+a[no];
no++;
}
System.out.println("sum="+sum);
System.out.println("avg="+sum/100);
}
}

public class e8a3


{
public static void main(String args[])throws InterruptedException
{
thread1 t1=new thread1("g",1);
t1.t.join();

thread1 t2=new thread1("r",100);


t2.t.join();

thread1 t3=new thread1("s",200);


t3.t.join();

thread1 t4=new thread1("t",300);


t4.t.join();

thread1 t5=new thread1("p",400);


t5.t.join();
thread1 t6=new thread1("p",500);
t5.t.join();

thread1 t7=new thread1("p",600);


t5.t.join();

thread1 t8=new thread1("p",700);


t5.t.join();

thread1 t9=new thread1("p",800);


t5.t.join();

thread1 t10=new thread1("p",900);


t5.t.join();

}
}

-----------------------------------------------------------------------------------
----

set B:

2)

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;

/*<applet code="e8b2.class" width="300" height="300"></applet>*/

class Thr extends Thread


{
boolean up=false;
e8b2 parent;
int top,left;

Color c;
Thr(int t,int l,Color cr,e8b2 p)
{
top=1;
if(top>170)
top=170-(t/8);
left=t;
c=cr;
parent=p;
}

public void run()


{
try
{
while(true)
{
Thread.sleep(37);
if(top>=100)
up=true;
if(top<=0)
up=false;
if(!up)
top=top+2;
else
top=top-2;
parent.p.repaint();
}
}

catch(Exception e)
{
}
}
}

public class e8b2 extends JFrame implements ActionListener


{
int top=0,left=0,n=0,radius=50;
Color
c[]={Color.black,Color.cyan,Color.red,Color.orange,Color.yellow,Color.pink,Color.gr
ay,Color.blue,Color.green,Color.magenta};

Thr t[]=new Thr[10];


GPanel p;
JButton b;
Panel p1;

e8b2()
{
setSize(700,300);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(p=new GPanel(this),BorderLayout.CENTER);
b=new JButton("start");
b.addActionListener(this);
setVisible(true);
add(p1=new Panel(),BorderLayout.SOUTH);
p1.setBackground(Color.lightGray);
p.add(b);
setVisible(true);
}

public static void main(String args[])


{
new e8b2();
}

public void actionPerformed(ActionEvent e)


{
t[n]=new Thr(left+(radius+13)*n+89,top+n*25,c[n],this);
t[n].start();
n++;
p.repaint();
if(n>9)
b.setEnabled(false);
}
}

class GPanel extends JPanel


{
e8b2 parent;

GPanel(e8b2 p)
{
parent=p;
}

public void paintComponent(Graphics g)


{
super.paintComponent(g);
setBackground(Color.white);
for(int i=0;i<parent.n;i++)
{
g.setColor(parent.t[i].c);

g.fillOval(parent.t[i].left,parent.t[i].top,parent.radius,parent.radius);
}
}
}

-----------------------------------------------------------------------------------
-------

set C:

1)

import java.io.*;

class fileThread extends Thread


{
String name;

public fileThread(String name)


{
this.name=name;
}

public void run()


{
File f=new File(name);
while(!f.exists())
{
try
{
System.out.println("File not exists"+name);
Thread.sleep(200);
}
catch(InterruptedException ie)
{
System.out.println("File not exists...."+ie);
}

System.out.println("File exists....."+name);
}
}
}

public class e8c1


{
public static void main(String args[])
{
fileThread f[]=new fileThread[args.length];
for(int i=0;i<args.length;i++)
{
f[i]=new fileThread(args[i]);
f[i].start();
}
}
}

===================================================================================
============

You might also like