Professional Documents
Culture Documents
Q1. Write a multithreading program in Java to display all the vowels from a given string.
(Use Thread class) Slip 2
Solution:-
import java.io.*;
try{
Thread.sleep((int)(Math.random()*1000));
}catch(Exception e){}
}
}
}
class VowelsThreadTest{
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter string:");
String s = br.readLine();
Q2. Write a Java program to simulate traffic signal using multithreading. Slip3
Solution:-
import java.awt.*;
import java.awt.event.*;
public TrafficSignal(){
t = new MyThread();
t.start();
setTitle("Traffic Signal");
setSize(150,500);
setVisible(true);
setBackground(Color.BLACK);
}
switch(f){
case 0:
g.setColor(Color.RED);
g.fillOval(70,200,w,h);
g.setColor(Color.WHITE);
g.fillOval(70,h+200,w,h);
g.fillOval(70,2*h+200,w,h);
break;
case 1:
g.setColor(Color.YELLOW);
g.fillOval(70,h+200,w,h);
g.setColor(Color.WHITE);
g.fillOval(70,200,w,h);
g.fillOval(70,2*h+200,w,h);
break;
case 2:
g.setColor(Color.GREEN);
g.fillOval(70,2*h+200,w,h);
g.setColor(Color.WHITE);
g.fillOval(70,200,w,h);
g.fillOval(70,h+200,w,h);
break;
}
}
Q3. Write a Multi-threading program in Java using Runnable interface to draw temple flag
on an applet container. Slip5
Solution:-
import java.awt.*;
import java.applet.*;
/*
<applet code=TempleApplet width=400 height=400></applet>
*/
//Execution:- after compiling appletviewer TempleApplet.java
TYBCA Practical On Multithreading (File2)
Q4. Write a Multithreading program in Java for racing car. (Use AWT) slip7
Solution:-
import java.awt.*;
import java.awt.event.*;
public CarRace(){
setTitle("Car Racing");
setSize(500,300);
setLocation(100,100);
setVisible(true);
t1 = new CarThread();
t2 = new CarThread();
t1.start();
t2.start();
g.setColor(Color.RED);
g.fillRect(t1.getX(),70,75,45);
g.setColor(Color.GREEN);
g.fillRect(t2.getX(),150,75,45);
}
try{
Thread.sleep((int)(Math.random()*1000));
}catch(Exception e){}
repaint();
}
}
Q5.Write a multithreading program using Runnable interface to blink Text on the frame.
Solution:- Slip 8
import java.awt.*;
import java.awt.event.*;
public CarRace(){
setTitle("Car Racing");
setSize(500,300);
setLocation(100,100);
setVisible(true);
t1 = new CarThread();
t2 = new CarThread();
t1.start();
t2.start();
g.setColor(Color.RED);
g.fillRect(t1.getX(),70,75,45);
g.setColor(Color.GREEN);
g.fillRect(t2.getX(),150,75,45);
}
try{
Thread.sleep((int)(Math.random()*1000));
}catch(Exception e){}
repaint();
}
}
Q6. Write a Multithreading program in java using Runnable interface to move text on the
frame as follow: Starting Position of Text (See diagram in Practical slip)
Sol:- slip 9
import java.awt.*;
import java.awt.event.*;
public MovingText(){
setTitle("Moving Text");
setSize(400,400);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
w = getWidth();
h = getHeight();
x = 0;
y = h/2;
}
}
Q7. Write a Multithreading program in Java for bouncing ball. For each bounce, change the
color of ball randomly. Slip 10
Sol:-
import java.awt.*;
import java.awt.event.*;
public BouncingBall(){
setTitle("Bouncing Balling");
setSize(400,400);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
w = getWidth();
h = getHeight();
x = (int)(Math.random()*w);
y = (int)(Math.random()*h);
(int)(Math.random()*256));
f=1;
}
break;
case 1:
y--;
if(y<0){
c = new Color((int)(Math.random()*256),
(int)(Math.random()*256),
(int)(Math.random()*256));
f=0;
}
}
repaint();
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
Q8. Write a multithreading program in Java to display the numbers between 1 to 100
continuously in a TextField by clicking on button (Use Runnable interface) . Slip 12
Solution:-
import java.awt.*;
import java.awt.event.*;
public CounterThread(){
txtCount = new TextField(10);
btnStart = new Button("Start");
setTitle("Counter");
setSize(300,400);
setLayout(new FlowLayout());
add(txtCount);
add(btnStart);
setVisible(true);
t = new Thread(this);
TYBCA Practical On Multithreading (File2)
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
btnStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
t.start();
}
});
}
try{
Thread.sleep(1000);
}catch(Exception e){}
}
}
Q9. Write a program in Java which will show lifecycle (creation, sleep, and dead) of a
thread. Program should print randomly the name of thread and value of sleep time. The
name of the thread should be hard coded through constructor. The sleep time of a thread
will be a random integer in the range 0 to 4999. Slip16
Sol:-
class ThreadLifeCycle{
public static void main(String args[]){
MyThread t1 = new MyThread("Ram"),
t2 = new MyThread("Seeta");
t1.start();
t2.start();
try{
t1.join();
t2.join();
}catch(Exception e){}
Q10. Write a Java program using multithreading to execute the threads sequentially (Use
Synchronized Method) slip18 (also IMP for theory exam)
Sol:-
class Test{
public static synchronized void print(){
for(int i=1;i<=10;i++){
System.out.println(Thread.currentThread());
try{
Thread.sleep((int)(Math.random()*1000));
}catch(Exception e){}
}
}
}
class MyThread extends Thread{
public void run(){
Test.print();
}
}
class SynchronizedDemo{
public static void main(String args[]){
new MyThread().start();
new MyThread().start();
}
}
Q11. Write a Multithreading program in Java to convert smile face into the crying face
after 5 seconds and viceversa (Use Applet). Slip19
Sol:
import java.awt.*;
import java.applet.*;
int w = getWidth();
int h = getHeight();
g.drawOval(20,20,w-40,h-40);
g.drawOval(w/2-75,h/2-75,30,30);
g.drawOval(w/2+75,h/2-75,30,30);
if(flag){
g.drawArc(w/2-75,h/2-30,150,60,180,180);
}
else{
g.drawArc(w/2-75,h/2-30,150,60,180,-180);
}
}
}
/*
<applet code=FaceApplet width=400 height=400></applet>
*/
Q12. Write a multithreading program in Java to create an applet that contains a TextField
to show time. The time should be displayed in the hh:mm:ss format. The thread should
start when the user clicks the Start button and stop when the user clicks the stop
button. Initialize the values to current time. [25]
Sol:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
private MyThread t;
btnStart.setMnemonic('S');
btnStop.setMnemonic('o');
btnStart.setToolTipText("Resume watch");
btnStop.setToolTipText("Suspend watch");
setLayout(new FlowLayout());
add(txtTime);
add(btnStart);
add(btnStop);
t = new MyThread();
t.start();