0% found this document useful (0 votes)
8 views2 pages

Synchronization WithSyncMethod

The document contains Java code for a multithreaded application that demonstrates synchronization using the Count class. The Count class allows for incrementing and decrementing a value safely across multiple threads using synchronized methods. The Adder and Subtractor classes implement Runnable to modify the Count instance concurrently, showcasing thread safety in operations.

Uploaded by

abhinavstar3909
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Synchronization WithSyncMethod

The document contains Java code for a multithreaded application that demonstrates synchronization using the Count class. The Count class allows for incrementing and decrementing a value safely across multiple threads using synchronized methods. The Adder and Subtractor classes implement Runnable to modify the Count instance concurrently, showcasing thread safety in operations.

Uploaded by

abhinavstar3909
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

package WithSyncMethod;

import [Link].*;
import [Link];
import [Link];

public class Client {


public static void main(String[] args) throws Exception{
Count count=new Count();

Adder ad=new Adder(count);


Subtractor sb=new Subtractor(count);

Thread t1=new Thread(ad);


Thread t2=new Thread(sb);

[Link]();
[Link]();

[Link]();
[Link]();

[Link]();
}
}
package WithSyncMethod;

public class Count {


private int value=0;
public synchronized void incrval(int x){
value+=x;
}
public void getval(){
[Link]([Link]);
}
}
//OR
//public class Count {
// private int value=0;
// public synchronized void incrval(int x){
// [Link]+=x;
// }
// public synchronized void decrval(int x){
// [Link]-=x;
// }
// public void getval(){
// [Link]([Link]);
// }
//}
package WithSyncMethod;
import [Link];
public class Adder implements Runnable{
private Count count;
public Adder(Count count){
[Link]=count;
}
@Override
public void run() {
for(int i=0;i<10000;i++){
[Link](i);
}
}
}
package WithSyncMethod;
import [Link];

public class Subtractor implements Runnable{


private Count count;

public Subtractor(Count count){


[Link]=count;
}
@Override
public void run() {
for(int i=0;i<10000;i++){
[Link](-i);
}
}
}

You might also like