-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyThread.java
More file actions
81 lines (80 loc) · 2.07 KB
/
MyThread.java
File metadata and controls
81 lines (80 loc) · 2.07 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
public class MyThread {
public static void main(String[] args) {
WhiteBoard wb=new WhiteBoard();
Teacher t=new Teacher(wb);
Student sam=new Student("samihan", wb);
Student ved=new Student("vedant", wb);
Student vrd=new Student("varad", wb);
t.start();
sam.start();
ved.start();
vrd.start();
}
}class WhiteBoard{
String text;
int numberOfStudents=0;
int count=0;
public void attendance(){
numberOfStudents++;
}
synchronized public void write(String t){
System.out.println("Teacher is Writing " +t);
while(count!=0)
try{wait();}catch(Exception e){}
text=t;
count=numberOfStudents;
notifyAll();
}
synchronized public String read(){
while(count==0)
try{wait();}catch(Exception e){}
String t=text;
count--;
if(count==0)
notify();
return t;
}
}
class Teacher extends Thread{
WhiteBoard wb;
String notes[]={"Java is language","It is OOPs","It is Platform Independent","It supports Thread","end","great"};
public Teacher(WhiteBoard w){
wb=w;
}
public void run(){
for(int i=0;i<notes.length;i++)
wb.write(notes[i]);
}
}
class Student extends Thread{
String name;
WhiteBoard wb;
public Student(String name,WhiteBoard wb){
this.name=name;
this.wb=wb;
}
public void run(){
String text;
wb.attendance();
text=wb.read();
System.out.println(name + " Reading " + text);
System.out.flush();
if(text.equals("end"))return;
}
}
class Customer extends Thread{
int atm;
String name;
int amount;
public Customer(String name,int amount,int atm){
this.name=name;
this.amount=amount;
this.atm=atm;
}
// public void useATM(String trasaction){
// if(tr)
// }
// public void run(){
// useATM();
// }
}