FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-code-example/!temp/src/TestSwing.java at master · cherkavi/java-code-example · GitHub
cherkavi
/
java-code-example
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
0
Code
Issues
0
Pull requests
42
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-code-example
/
!temp
/
src
/
TestSwing.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
95 lines (85 loc) · 2.51 KB
Breadcrumbs
java-code-example
/
!temp
/
src
/
TestSwing.java
Copy path
File metadata and controls
executable file
·
95 lines (85 loc) · 2.51 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import
javax
.
swing
.*;
import
java
.
awt
.
event
.
ActionEvent
;
import
java
.
awt
.
event
.
ActionListener
;
import
java
.
awt
.
GridLayout
;
import
java
.
awt
.
BorderLayout
;
public
class
TestSwing
extends
JFrame
{
public
static
void
main
(
String
[]
args
){
new
TestSwing
();
}
private
final
static
long
serialVersionUID
=
1L
;
public
TestSwing
(){
super
(
"TestSwing"
);
this
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
initComponents
();
this
.
setSize
(
300
,
200
);
this
.
setVisible
(
true
);
}
/** ïåðâîíà÷àëüíàÿ èíèöèàëèçàöèÿ êîìïîíåíòîâ */
private
void
initComponents
(){
// create component's
JButton
buttonRun
=
new
JButton
(
"Run thread"
);
JButton
buttonGarbage
=
new
JButton
(
"Garbage"
);
JButton
buttonStop
=
new
JButton
(
"Stop thread"
);
// add listener's
buttonRun
.
addActionListener
(
new
ActionListener
(){
public
void
actionPerformed
(
ActionEvent
e
){
onButtonRun
();
}
});
buttonGarbage
.
addActionListener
(
new
ActionListener
(){
public
void
actionPerformed
(
ActionEvent
e
){
onButtonGarbage
();
}
});
buttonStop
.
addActionListener
(
new
ActionListener
(){
public
void
actionPerformed
(
ActionEvent
e
){
onButtonStop
();
}
});
// placing component's
JPanel
panelMain
=
new
JPanel
(
new
BorderLayout
());
JPanel
panelManager
=
new
JPanel
(
new
GridLayout
(
3
,
1
));
panelManager
.
add
(
buttonRun
);
panelManager
.
add
(
buttonGarbage
);
panelManager
.
add
(
buttonStop
);
panelMain
.
add
(
panelManager
);
this
.
getContentPane
().
add
(
panelMain
);
}
private
void
onButtonRun
(){
//TestThread thread=
new
TestThread
();
}
private
void
onButtonGarbage
(){
Runtime
.
getRuntime
().
gc
();
}
private
void
onButtonStop
(){
}
}
/** êëàññ, êîòîðûé çàïóñêàåò ïîòîê, ïðè ÷åì ñîçäàåòñÿ ïîòîê ñ ïîìîùüþ èíòåðôåéñà Runnable,
* íî ññûëêà íà Thread(this) ëîêàëüíà â êîíñòðóêòîðå, òî åñòü íå ïðèâÿçàíà ê êëàññó,
* à òåîðåòè÷åñêè äîëæíà áûòü óáðàíà êîíñòðóêòîðîì ÷åðåç íåêîòîðîå âðåìÿ ïîñëå ñîçäàíèå äàííîãî êëàññà */
class
TestThread
implements
Runnable
{
private
boolean
flagRun
=
false
;
public
TestThread
(){
Thread
thread
=
new
Thread
(
this
);
this
.
flagRun
=
true
;
thread
.
start
();
}
public
void
stop
(){
this
.
flagRun
=
false
;
}
public
void
run
(){
int
counter
=
0
;
while
(
flagRun
==
true
){
System
.
out
.
println
(
"TestThread:"
+(
counter
++));
try
{
Thread
.
sleep
(
250
);
}
catch
(
InterruptedException
ex
){
System
.
out
.
println
(
"InterrruptedException: "
+
ex
.
getMessage
());
}
catch
(
Exception
ex
){
System
.
out
.
println
(
"Exception "
+
ex
.
getMessage
());
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL