-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78 .Multi-Threading.java
More file actions
77 lines (69 loc) · 1.85 KB
/
78 .Multi-Threading.java
File metadata and controls
77 lines (69 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package test;
//import javax.swing.*;
//import java.lang.Math;
//import java.util.Random;
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//import java.util.HashMap;
import java.io.*;
import java.util.*;
import java.util.Timer;
//import javax.sound.sampled.*;
import java.awt.*; // abstract window toolkit
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import javax.swing.*;
//import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.imageio.ImageIO;
public class main {
// Multi-Threading .
// Multi-Threading :
public static void main(String[] args) {
// TODO Auto-generated method stub
// Declare the Scanner library
Scanner scanner = new Scanner(System.in);
// Create and start the threads
Thread thread1 = new threader("Thread 1");
Thread thread2 = new threader("Thread 2");
thread1.start();
thread2.start();
// Wait for the threads to finish
try {
thread1.join();
thread2.join();
}catch (Exception e){
e.printStackTrace();
}
// Print out a message when both threads are done
System.out.println("Finished...");
}
private static class threader extends Thread{
private String name;
// default constructor
public threader(String name) {
this.name = name;
}
public void run() {
for(int i = 0 ; i < 5; i++) {
System.out.println(name + " is running..." + i);
try {
sleep(1000); // sleep for 1 second
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}