Upload files to "OOP/Java/Lab/Week11"
This commit is contained in:
parent
a6a0f6877f
commit
f263392866
5 changed files with 427 additions and 0 deletions
44
OOP/Java/Lab/Week11/syncStatements.java
Normal file
44
OOP/Java/Lab/Week11/syncStatements.java
Normal file
|
@ -0,0 +1,44 @@
|
|||
class IncrementThread extends Thread{
|
||||
Incrementer ref;
|
||||
IncrementThread(Incrementer obj)
|
||||
{
|
||||
ref=obj;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
for(int i=0;i<10000;i++)
|
||||
{
|
||||
ref.increment();
|
||||
}
|
||||
}
|
||||
}
|
||||
class Incrementer{
|
||||
int count;
|
||||
Incrementer(int cnt)
|
||||
{
|
||||
count=cnt;
|
||||
}
|
||||
void increment()
|
||||
{
|
||||
synchronized(this)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
class syncMethod{
|
||||
public static void main(String[] args) {
|
||||
Incrementer inc=new Incrementer(0);
|
||||
IncrementThread t1=new IncrementThread(inc);
|
||||
IncrementThread t2=new IncrementThread(inc);
|
||||
t1.start();
|
||||
t2.start();
|
||||
try {
|
||||
t1.join();
|
||||
t2.join();
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println(e.toString());
|
||||
}
|
||||
System.out.println(inc.count);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue