-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path26_Threads in java
More file actions
176 lines (147 loc) · 3.82 KB
/
26_Threads in java
File metadata and controls
176 lines (147 loc) · 3.82 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// threads
package com.company;
class MyThread extends Thread {
public void run(){
int i = 0;
while (i<2000) {
System.out.println("hello");
System.out.println("World");
i++;
}
}
}
class MyThread2 extends Thread{
public void run(){
int i = 0;
while (i<2000) {
System.out.println("Thread 2");
System.out.println("New thread");
i++;
}
}
}
public class Threads {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread2 t2 = new MyThread2();
t1.start();
t2.start();
}
}
// Runnable threads
package com.company;
class MyThreadRunnable implements Runnable{
public void run(){
System.out.println("Running thread");
}
}
class MyThreadRunnable2 implements Runnable{
public void run(){
System.out.println("Running thread 2");
}
}
public class Runnable_threads {
public static void main(String[] args) {
MyThreadRunnable mth = new MyThreadRunnable();
Thread pm = new Thread(mth);
MyThreadRunnable2 mth2 = new MyThreadRunnable2();
Thread gg = new Thread(mth2);
pm.start();
gg.start();
}
}
//Thread constructor
package com.company;
class MyThr extends Thread{
public MyThr(String name){
super(name);
}
public void run(){
int i = 30;
System.out.println("Thank you");
// while (true){
// System.out.println("I am a thread");
// }
}
}
public class Thread_constructor {
public static void main(String[] args) {
MyThr mt = new MyThr("PM");
MyThr tm = new MyThr("GG");
mt.start();
System.out.println("The id of the thread is "+ mt.getId());
System.out.println("The name of the thread is "+ mt.getName());
System.out.println("The id of the thread is "+ tm.getId());
System.out.println("The name of the thread is "+ tm.getName());
}
}
//Thread priorities
package com.company;
class MyThr1 extends Thread{
public MyThr1(String name){
super(name);
}
public void run(){
int i = 30;
System.out.println("Thank you "+this.getName());
while (true){
// System.out.println("I am a thread");
System.out.println("Thank you "+this.getName());
}
}
}
public class Thread_priorities {
public static void main(String[] args) {
MyThr1 mt = new MyThr1("PM");
MyThr1 tm = new MyThr1("GG");
MyThr1 gm = new MyThr1("GM");
MyThr1 nw = new MyThr1("TM");
nw.setPriority(Thread.MAX_PRIORITY);
mt.setPriority(Thread.MIN_PRIORITY);
tm.setPriority(Thread.MIN_PRIORITY);
gm.setPriority(Thread.MIN_PRIORITY);
mt.start();
tm.start();
gm.start();
nw.start();
}
}
//Thread methods
package com.company;
class MyThr2 extends Thread{
public void run(){
int i=0;
while (i<3333) {
System.out.println("Thank you " + this.getName());
i++;
}
while (true){
// System.out.println("I am a thread");
System.out.println("Thank you "+this.getName());
}
}
}
class MyThr3 extends Thread{
public void run(){
int i = 30;
System.out.println("Thank you "+this.getName());
while (true){
// System.out.println("I am a thread");
System.out.println("Thank you "+this.getName());
}
}
}
public class Thread_methods {
public static void main(String[] args) {
MyThr2 t1 = new MyThr2();
MyThr3 t2 = new MyThr3();
t1.start();
try {
t1.join();
}
catch (Exception e){
System.out.println(e);
}
t2.start();
}
}