Pages

Monday, 7 March 2011

Sem 2 JAVA THREAD


/***************************************************************
NAME     :
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :
Write a program that create 2 threads – each displaying a message (Pass the message as a parameter to the constructor). The threads should display the messages continuously till the user presses ctrl-c. Also display the thread information as it is running.
***************************************************************/

import java.io.*;
class Ass_seta1 extends Thread
{
String msg="";
Ass_seta1(String msg)
{
this.msg=msg;
}
public void run()
{
try
{ while(true)
{
System.out.println(msg);
Thread.sleep(200);
}
}
catch(Exception e){}
}
}
public class seta1
{
public static void main(String a[])
{
Ass_seta1 t1=new Ass_seta1("Hello............");
t1.start();
}
}

/******************************output***************************
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
Hello............
-------------------------------------------------------------*/
*******************************************************************************

/***************************************************************
NAME     :
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :

Define a thread called “PrintText_Thread” for printing text on command prompt forn number of times. Create three threads and run them. Pass the text and n as parameters to the thread constructor. Example:
i.   First thread prints “I am in FY” 10 times
ii. Second thread prints “I am in SY” 20 times
iii. Third thread prints “I am in TY” 30 times
***************************************************************/

import java.io.*;
import java.lang.String.*;

class Ass_seta3 extends Thread
{
String msg="";
int  n;
Ass_seta3(String msg,int n)
{
this.msg=msg;
this.n=n;
}
public void run()
{
try
{ for(int i=1;i<=n;i++)
{
System.out.println(msg+" "+i+" times");
}
}
catch(Exception e){}
}
}
public class seta3
{
public static void main(String a[])
{
int n=Integer.parseInt(a[0]);
Ass_seta3 t1=new Ass_seta3("I am in FY",n);
t1.start();
Ass_seta3 t2=new Ass_seta3("I am in SY",n+10);
t2.start();
Ass_seta3 t3=new Ass_seta3("I am in TY",n+20);
t3.start();
}
}


**********************************************************************************


/***************************************************************
NAME     :
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :
Define a thread to move a ball inside a panel vertically.The Ball should be created when user clicks on the Start Button. Each ball should have a different color and vertical position(calculated randomly). Note: Suppose user has clicked buttons 5 times then five balls should be created and move inside the panel. Ensurethat ball is moving within the panel border only.
***************************************************************/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class boucingthread extends JFrame implements Runnable
{
Thread t;
int x,y;

boucingthread()
{
super();
t= new Thread(this);
x=10;
y=10;
t.start();
setSize(1000,200);
setVisible(true);
setTitle("BOUNCEING BOLL WINDOW");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run()
{
try
{
while(true)
{
x+=10;
y+=10;
repaint();
Thread.sleep(1000);
}
}catch(Exception e)
{

}
}

public void paint(Graphics g)
{

g.drawOval(x,y,7,7);

}

public static void main(String a[])throws Exception
{
boucingthread t=new boucingthread();
Thread.sleep(1000);
}
}



***************************************************************************************





No comments:

Post a Comment