-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.txt
More file actions
252 lines (229 loc) · 3.88 KB
/
thread.txt
File metadata and controls
252 lines (229 loc) · 3.88 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
PRODUCER
package thread;
public class Q {
int n;
boolean valueset=false;
synchronized int get()
{
while(!valueset)
{
try
{
wait();}
catch(InterruptedException e) {}
}
System.out.println("Got: "+n);
valueset=false;
notify();
return n;
}
synchronized void put(int n)
{
while(valueset)
{
try
{
wait();
}catch(InterruptedException e)
{}
this.n=n;
valueset=true;
System.out.println("Put: "+n);
notify();
}
}
}
package thread;
public class Producer implements Runnable {
Q q;
Producer(Q q)
{
this.q=q;
new Thread(this,"Producer").start();
}
@Override
public void run() {
// TODO Auto-generated method stub
int i=0;
while(true)
{
q.put(i++);
}
}
}
package thread;
public class Consumer implements Runnable {
Q q;
Consumer(Q q)
{
this.q=q;
new Thread(this,"Consumer").start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
q.get();
}
}
}
package thread;
public class PCFIxed {
public static void main(String args[])
{
// TODO Auto-generated method stub
Q q=new Q();
new Producer(q);
new Consumer(q);
System.out.println("what");
}}
SYNCHONIZED
package thread;
public class Synch {
public static void main(String args[])
{
Callme target =new Callme();
Caller ob1=new Caller(target,"Hello");
Caller ob2=new Caller(target,"World");
try
{
ob1.t.join();
ob2.t.join();
}
catch(InterruptedException e) {}
}
}
package thread;
public class Caller implements Runnable {
String msg;
Callme target;
Thread t;
public Caller(Callme targ,String s)
{
target=targ;
msg=s;
t=new Thread(this);
t.start();
}
public void run ()
{
target.call(msg);
}
}
package thread;
public class Callme {
synchronized void call(String msg) {
System.out.println("["+msg);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interruted");
}
System.out.println("]");
}
}
ALIVEJOIN
package thread;
public class AliveJoin extends Thread{
public static void main(String[] args) {
// TODO Auto-generated method stub
AliveJoin t1=new AliveJoin();
AliveJoin t2=new AliveJoin();
t1.start();
try
{
t1.join();
}catch(InterruptedException e)
{}
t2.start();
//threadc.setPrioroty(Thread.MAX_PRIOPITY); threadq.getPriority();
}
public void run()
{
System.out.println("r1");
try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
System.out.println("r2");
}
}
}
TEST
package thread;
//public class NewThread implements Runnable {
//NewThread()
//{
//Thread t=new Thread(this,"Sec Thread");
//System.out.println("Child: "+t);
//t.start();
//}
// @Override
// public void run() {
// // TODO Auto-generated method stub
//
// try {
// for(int i=5;i>0;i--)
// {
// System.out.println("Child Thread: "+i);
// Thread.sleep(500);
// }
// }
// catch(InterruptedException e)
// {
// System.out.println("Child thread interrupted");
// }
// System.out.println("Exiting child thread");
// }
//
//}
class NewThread extends Thread
{
NewThread()
{
super("Child Thread");
System.out.println("Child: "+this);
start();
}
public void run()
{
try {
for(int i=5;i>0;i--)
{
System.out.println("Child Thread: "+i);
Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("Child thread interrupted");
}
System.out.println("Exiting child thread");
}
}
package thread;
public class ThreadDemo {
public static void main(String args[])
{
new NewThread();
try
{
for(int i=5;i>0;i--)
{
System.out.println("Main: "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main thread over");
}
}