FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaSE/JavaThread/MultiThread.java at master · goder/JavaSE · GitHub
goder
JavaSE
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaSE
/
JavaThread
/
MultiThread.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (64 loc) · 1.2 KB
Breadcrumbs
JavaSE
/
JavaThread
/
MultiThread.java
Copy path
File metadata and controls
73 lines (64 loc) · 1.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* 功能:两个线程一起运行,启动进行测试
*/
package
com
.
bj
.
thread
;
public
class
MultiThread
{
public
static
void
main
(
String
[]
args
) {
// TODO Auto-generated method stub
Pig
pig
=
new
Pig
(
10
);
Bird
bird
=
new
Bird
(
10
);
Thread
t1
=
new
Thread
(
pig
);
Thread
t2
=
new
Thread
(
bird
);
t1
.
start
();
t2
.
start
();
}
}
class
Pig
implements
Runnable
{
int
n
=
0
;
int
times
=
0
;
public
Pig
(
int
n
){
this
.
n
=
n
;
}
public
void
run
(){
while
(
true
){
try
{
Thread
.
sleep
(
1000
);
}
catch
(
InterruptedException
e
) {
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
times
++;
System
.
out
.
println
(
"我是一个线程,在输出第"
+
times
+
"个Pig"
);
if
(
times
==
n
){
System
.
out
.
println
(
"结果是"
+
times
);
break
;
}
}
}
}
class
Bird
implements
Runnable
{
int
n
=
0
;
int
res
=
0
;
int
times
=
0
;
public
Bird
(
int
n
){
this
.
n
=
n
;
}
@
Override
public
void
run
() {
// TODO Auto-generated method stub
while
(
true
){
try
{
Thread
.
sleep
(
1000
);
}
catch
(
InterruptedException
e
) {
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
res
+=(++
times
);
System
.
out
.
println
(
"Bird当前结果:"
+
res
);
if
(
times
==
n
){
System
.
out
.
println
(
"最后结果是"
+
res
);
break
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL