0% found this document useful (0 votes)
9 views6 pages

Multithreading and Stopwatch GUI Example

The document describes two experiments related to multithreading in Java. The first experiment implements a simple thread using the Runnable interface, demonstrating concurrent counting with two threads. The second experiment creates a Swing GUI stopwatch that supports lap counting and multithreading for accurate time tracking.

Uploaded by

saritasc1205
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Multithreading and Stopwatch GUI Example

The document describes two experiments related to multithreading in Java. The first experiment implements a simple thread using the Runnable interface, demonstrating concurrent counting with two threads. The second experiment creates a Swing GUI stopwatch that supports lap counting and multithreading for accurate time tracking.

Uploaded by

saritasc1205
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment No - 10

Problem Statement:
1. Program to implement thread using runnable interface.

class Experiment_No_10_1st_MyRunnable implements Runnable


{ public void
run()
{ for (int i = 1; i <= 5;
i++)
{
[Link]([Link]().getName() + " - Count: " + i);

try
{
[Link](500);
}
catch (InterruptedException e)
{
[Link](e);
}
}
}
public static void main(String args [])
{
Experiment_No_10_1st_MyRunnable obj = new Experiment_No_10_1st_MyRunnable();

Thread t1 = new Thread(obj);


Thread t2 = new Thread(obj);
[Link]("Thread - 1");
[Link]("Thread - 2");

[Link]();
[Link]();
}
}
Output :-
Thread - 1 - Count: 1
Thread - 2 - Count: 1
Thread - 1 - Count: 2
Thread - 2 - Count: 2
Thread - 2 - Count: 3
Thread - 1 - Count: 3
Thread - 1 - Count: 4
Thread - 2 - Count: 4
Thread - 2 - Count: 5
Thread - 1 - Count: 5
2. Create Stop Watch with Swing GUI and Multithreading. Provide Facility
for Lap Counting.

import [Link].*;
import [Link].*; import
[Link].*; import
[Link];

public class Experiment_No_10_2nd_StopWatch extends JFrame implements ActionListener


{
JLabel timeLabel;
JButton startBtn, stopBtn, lapBtn, resetBtn;
JTextArea lapArea;
JScrollPane scrollPane;

boolean running = false;


long startTime; long
elapsedTime = 0;
Thread timerThread;
ArrayList<String> laps = new ArrayList<>();

public Experiment_No_10_2nd_StopWatch()
{
setTitle("Stopwatch with Lap");
setSize(400, 400); setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

timeLabel = new JLabel("00:00:00");


[Link](new Font("Arial", [Link], 40));
[Link](100, 20, 200, 50);
[Link]([Link]);
add(timeLabel);

startBtn = new JButton("Start");


stopBtn = new JButton("Stop"); lapBtn =
new JButton("Lap"); resetBtn = new
JButton("Reset");

[Link](30, 90, 80, 30);


[Link](120, 90, 80, 30); [Link](210,
90, 80, 30); [Link](300, 90, 80, 30);

add(startBtn);
add(stopBtn); add(lapBtn);
add(resetBtn);

lapArea = new JTextArea();


[Link](false); scrollPane =
new JScrollPane(lapArea);
[Link](30, 140, 320, 200);
add(scrollPane);

[Link](this);
[Link](this); [Link](this);
[Link](this);

setVisible(true);
}

@Override public void


actionPerformed(ActionEvent e)
{ if ([Link]() ==
startBtn)
{ if (!running) { running = true; startTime =
[Link]() - elapsedTime; // resume support startTimer();

}
}
else if ([Link]() == stopBtn) { running = false; elapsedTime
= [Link]() - startTime; // save elapsed time
}
else if ([Link]() == lapBtn) {
if (running) {
String currentTime = [Link]();
[Link](currentTime); [Link]("Lap " + [Link]() + ":
" + currentTime + "\n");
}
}
else if ([Link]() == resetBtn) {
running = false; elapsedTime =
0;
[Link]("00:00:00");
[Link](""); [Link]();
}
}

public void startTimer()


{
timerThread = new Thread(() ->
{ while (running) { long elapsed =
[Link]() - startTime;

long minutes = (elapsed / (1000 * 60)) % 60;


long seconds = (elapsed / 1000) % 60; long
millis = (elapsed % 1000) / 10;

String time = [Link]("%02d:%02d:%02d", minutes, seconds, millis);


[Link](time);

try {
[Link](50);
}
catch (InterruptedException ex)
{ [Link]();
}
}
});
[Link]();
}

public static void main(String[] args)


{
Experiment_No_10_2nd_StopWatch experiment_No_10_2nd_StopWatch = new
Experiment_No_10_2nd_StopWatch();
}
}
Output :-

Start: Lap:

Clear:

You might also like