-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadexamples.java
More file actions
83 lines (70 loc) · 2.28 KB
/
threadexamples.java
File metadata and controls
83 lines (70 loc) · 2.28 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
// java thread
class MyThread1 extends Thread {
static Thread ob;
public void run(){
System.out.println("Table of 2:");
for(int i=1;i<=10;i++){
try {
Thread.sleep(500);
if (i==4) {
ob.interrupt();
continue;
}
} catch (InterruptedException e) {
System.out.println("Interruption!!!!!-1");
}
System.out.println(Thread.currentThread().getName()+" "+2+" * "+i+" = "+(2*i) );
}
}
}
class MyThread2 extends Thread{
static Thread ob;
public void run(){
try {
ob.join();
} catch (InterruptedException e) {
}
System.out.println("Table of 3:");
for(int i=1;i<=10;i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Interruption!!!!!-2");
}
System.out.println(3+" * "+i+" = "+(3*i) );
}
}
}
public class threadexamples{
public static void main(String[] args) {
// MyThread1 t1= new MyThread1();
// t1.start();
MyThread1 t1 = new MyThread1();
MyThread2 t2 = new MyThread2();
MyThread2.ob=t1;
MyThread1.ob=t1;
t1.start();
t2.start();
// for (int i = 0; i <=10; i++) {
// System.out.println("Main thread "+i);
// }
}
}
// Thread.getName() gets the current threads name from 0....to...
// Thread.setName("new name")---> setName(String name)--> set name for the thread
// Thread.currentThread() gets the current thread object || is used with getName()
// Thread.currentThread().getName()-->gets the name of thread executing currently
// thread scheduler : used for setting priority to threads
// Thread.MIN_PRIORITY -->1
// Thread.MAX_PRIORITY --> 10
// Thread.NORM_PRIORITY -->5
// t1.setPriority(5);
// t1.setPriority(Thread.NORM_PRIORITY)-->5;
// default priority for every thread 5
// child threads derives priority from the parent main thread--5
// Preventing execution of tread:
/* sleep(): pauses thread exceution for some time::
* sleep(miliseconds time value)-->sleep(1000)
* Thread.sleep(2000) delays till time and then executes.
* join():
*/