Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- public class ThreadTestTwo {
- public int a = 0, b = 0,c = 0;
- public static void main(String[] args) {
- System.out.println(new Date()+"start");
- for (int i = 0; i < 100000; i++) {
- new ThreadTestTwo().start(i);
- if(i % 100000 == 0){
- System.out.println(i/100000);
- }
- }
- System.out.println(new Date()+"finish");
- }
- public void start(final int i){
- Thread readThread = new Thread(){
- @Override
- public void run() {
- while (true) {
- if(c == 1){
- b = a;
- // System.out.println(i+", set b "+a);
- break;
- }
- // System.out.println(i + " run");
- }
- }
- };
- Thread writeThread = new Thread(){
- @Override
- public void run() {
- a = 1;
- c = 1;
- }
- };
- writeThread.setName("mywrite");
- readThread.setName("myread");
- System.out.println(i+" start");
- writeThread.start();
- readThread.start();
- try {
- writeThread.join();
- readThread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(i+" end");
- if(b != 1)
- throw new RuntimeException("b = "+b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement