-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithreads.tex
More file actions
84 lines (78 loc) · 1.64 KB
/
multithreads.tex
File metadata and controls
84 lines (78 loc) · 1.64 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
78
79
80
81
82
83
84
\pagebreak{}
\section{Multi-threading}
\begin{verbatim}
class A extends Thread
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
if (i == 1)
yield();
System.out.println("Thread from A: i:"+i);
}
System.out.println("exit from A");
}
}
class B extends Thread
{
public void run()
{
for (int j = 1; j <= 5; j++)
{
System.out.println("Thread from B: j"+j);
if (j == 3)
stop();
}
System.out.println("exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k = 1; k <= 5; k++)
{
System.out.println("Thread from C: k"+k);
if (k == 1)
try {
sleep(1000);
}
catch (Exception e) {
}
}
System.out.println("exit from C");
}
}
public class Mythread {
public static void main(String[] args) {
A a = new A();
B b = new B();
C c = new C();
System.out.println("Thread start A");
a.start();
System.out.println("Thread start B");
b.start();
System.out.println("Thread start C");
c.start();
}
}
\end{verbatim}
\section*{Output}
\begin{verbatim}
>: java Mythread
Thread start A
Thread start B
Thread start C
Thread from B: 1
Thread from B: 2
Thread from B: 3
Thread from C: 1
Thread from A: 1
Thread from A: 2
Thread from A: 3
Thread from A: 4
Thread from A: 5
exit from A
exit from C
\end{verbatim}