Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public int count = 0;
- public int count2 = 0;
- public synchronized void increment() {
- count++;
- }
- public synchronized void increment2() {
- count2++;
- }
- public void doSomething() {
- Thread t1 = new Thread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- for (int i = 0; i < 10000; i++)
- increment();
- }
- });
- Thread t2 = new Thread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- for (int i = 0; i < 10000; i++)
- increment2();
- }
- });
- t1.start();
- t2.start();
- try {
- t1.join();
- t2.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- Main ob = new Main();
- ob.doSomething();
- System.out.println(ob.count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement