-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynchornizeddemo.java
More file actions
49 lines (47 loc) · 1.43 KB
/
synchornizeddemo.java
File metadata and controls
49 lines (47 loc) · 1.43 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
/*Synchonized method : only one thread executes for method or block declared
* but does not work for 2 objects created : works for one object that has multiple threads
* Below two thread executes but the very first thread that starts completes its execution first
* either thread-0.......thread-1 or thread-1......thread-0 only these not random of both
*/
class myThread implements Runnable{
@Override
public void run(){
disp();
}
synchronized static public void disp(){
for (int i = 0; i <=10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread().getName());
}
}
}
public class synchornizeddemo{
public static void main(String[] args) {
myThread m1 = new myThread();
myThread m2 = new myThread();
Thread t1=new Thread(m1);
Thread t2=new Thread(m2);
t1.start();
t2.start();
}
}
/* Synchronized block : this can be added inside methods and will function(inside block) in synchronized way
* other than the block all executes in non synchronized way leading to messy output
* synchornized(this){
* for (int i = 0; i <=10; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("Synchronized area block ");
}
* }
*
*
*
*
*
*/