Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import java.util.Date;
  2.  
  3. public class ThreadTestTwo {
  4. public int a = 0, b = 0,c = 0;
  5.  
  6. public static void main(String[] args) {
  7. System.out.println(new Date()+"start");
  8. for (int i = 0; i < 100000; i++) {
  9. new ThreadTestTwo().start(i);
  10. if(i % 100000 == 0){
  11. System.out.println(i/100000);
  12. }
  13. }
  14. System.out.println(new Date()+"finish");
  15. }
  16.  
  17. public void start(final int i){
  18. Thread readThread = new Thread(){
  19. @Override
  20. public void run() {
  21. while (true) {
  22. if(c == 1){
  23. b = a;
  24. // System.out.println(i+", set b "+a);
  25. break;
  26. }
  27. // System.out.println(i + " run");
  28. }
  29. }
  30. };
  31. Thread writeThread = new Thread(){
  32. @Override
  33. public void run() {
  34. a = 1;
  35. c = 1;
  36. }
  37. };
  38. writeThread.setName("mywrite");
  39. readThread.setName("myread");
  40. System.out.println(i+" start");
  41. writeThread.start();
  42. readThread.start();
  43.  
  44. try {
  45. writeThread.join();
  46. readThread.join();
  47. } catch (InterruptedException e) {
  48. e.printStackTrace();
  49. }
  50. System.out.println(i+" end");
  51. if(b != 1)
  52. throw new RuntimeException("b = "+b);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement