forked from deepdalsania/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYield.java
More file actions
63 lines (52 loc) · 1.32 KB
/
Yield.java
File metadata and controls
63 lines (52 loc) · 1.32 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
package multithreading;
public class Yield {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/* Prio_Thread1 pt = new Prio_Thread1();
Prio_Thread2 pt1 = new Prio_Thread2();*/
Thread t = new Thread(new ThreadY());
t.setName("Lower Priority");
//t.setPriority(Thread.NORM_PRIORITY-3);
Thread t1 = new Thread(new ThreadY_1());
t1.setName("Higher Priority");
t1.setPriority(Thread.NORM_PRIORITY+3);
t.start();
t1.start();
Thread.yield();
}
}
class ThreadY implements Runnable
{
public void run() {
// TODO Auto-generated method stub
// System.out.println("<< Odd Numbers in Ascending Order >>");
for (int i = 1; i < 10; i=i+2) {
try {
System.out.println(Thread.currentThread().getName()+" "+"I = "+i);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO: handle exception
System.out.println(e);
}
}
}
}
class ThreadY_1 implements Runnable
{
public void run() {
// TODO Auto-generated method stub
//System.out.println("<< Even Numbers in Descending Order >>");
for (int j = 10; j > 1; j=j-2) {
try {
System.out.println(Thread.currentThread().getName()+" "+"J = "+j);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO: handle exception
System.out.println(e);
}
}
}
}