Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
47 lines (44 loc) · 1.62 KB

File metadata and controls

47 lines (44 loc) · 1.62 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
class ThreadB extends Thread //this thread performing updation so called notify method
{
int tot=0;
public void run()
{
synchronized(this)
{
/*1*/System.out.println("child thread start calculation");
for (int i = 1; i <= 100; i++) {
tot += i;
}
/*2*/System.out.println("Child thread trying to give notification");
/*3*/this.notify();//notify method
}
}
}
//Run this code and see o/p and also analyse
//most of time main thread get chance according to that flow indicated by numbers
/*
As main thread get chance but main sleep for 10 sec then child start
calculation after child send notification after that main thread is waiting
so we get o/p as
child thread start calculation
Child thread trying to give
notification main thread trying to call wait method
AND program is stucked here only
*/
class interthreadcommunication2 //this thred expecting updation so called wait method
{
public static void main(String[] args) throws InterruptedException
{
ThreadB b=new ThreadB();
b.start();
Thread.sleep(10000);
System.out.println("main thread come out of sleep");
synchronized(b)
{
/*4*/System.out.println("main thread trying to call wait method");
b.wait();//after calling this main thread go into waiting state
/*5(not execute)*/System.out.println("main thread got notificaion ");//as thread not get any notification
/*6(not execute)*/System.out.println(b.tot);
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.