Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. public class Main {
  2. public int count = 0;
  3. public int count2 = 0;
  4.  
  5. public synchronized void increment() {
  6. count++;
  7. }
  8.  
  9. public synchronized void increment2() {
  10. count2++;
  11. }
  12.  
  13. public void doSomething() {
  14. Thread t1 = new Thread(new Runnable() {
  15.  
  16. @Override
  17. public void run() {
  18. // TODO Auto-generated method stub
  19. for (int i = 0; i < 10000; i++)
  20. increment();
  21. }
  22. });
  23.  
  24. Thread t2 = new Thread(new Runnable() {
  25.  
  26. @Override
  27. public void run() {
  28. // TODO Auto-generated method stub
  29. for (int i = 0; i < 10000; i++)
  30. increment2();
  31. }
  32. });
  33.  
  34. t1.start();
  35. t2.start();
  36.  
  37. try {
  38. t1.join();
  39. t2.join();
  40. } catch (InterruptedException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44.  
  45. }
  46.  
  47. public static void main(String[] args) {
  48. Main ob = new Main();
  49. ob.doSomething();
  50. System.out.println(ob.count);
  51.  
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement