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

Synchronize WithSync

The document contains Java code for a multi-threaded application that uses synchronization to manage a shared resource, 'Count'. It defines classes for adding and subtracting values from a shared count variable in a thread-safe manner. The main method creates and starts threads for both operations and prints the final value of the count after both threads complete their execution.

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)
5 views2 pages

Synchronize WithSync

The document contains Java code for a multi-threaded application that uses synchronization to manage a shared resource, 'Count'. It defines classes for adding and subtracting values from a shared count variable in a thread-safe manner. The main method creates and starts threads for both operations and prints the final value of the count after both threads complete their execution.

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 WithonlySync;

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]([Link]);
}
}
package WithonlySync;

public class Count {


int value=0;
}
package WithonlySync;

public class Adder implements Runnable{


private Count count;

public Adder(Count count){


[Link]=count;

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

}
}
package WithonlySync;

public class Subtractor implements Runnable{


private Count count;

public Subtractor(Count count){


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

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

You might also like