-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadingCode.java
More file actions
43 lines (34 loc) · 899 Bytes
/
Copy pathMultiThreadingCode.java
File metadata and controls
43 lines (34 loc) · 899 Bytes
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
public class HelloWorld
{
volatile static int i = 0;
public static void main(String []args) throws Exception
{
int time = 0;
MyThread t = new MyThread();
t.start();
while (time < 20)
{
System.out.println("i=" + i);
Thread.sleep(500);
time +=1;
}
t.interrupt();
System.out.println("main thread done");
}
private static class MyThread extends Thread
{
public void run()
{
try
{
while (!isInterrupted())
{
i = i + 1;
Thread.sleep(1000);
}
}
catch (InterruptedException e) {System.out.println(e.getMessage());}
catch (Exception e) {System.out.println(e.toString());}
}
}
}