blocking

package blockingtest;

/**
* Hello world!
*/
public class Sync implements Runnable {
int n;

public static void main(String[] args) {
Sync sync = new Sync();
new Thread(sync, "Sync1").start();
new Thread(sync, "Sync2").start();
new Thread(sync, "Sync3").start();
}

public void run() {
for (int i = 0; i < 3; i++) {
inc();
}
}

public synchronized void inc() {
n++;
System.out.print(Thread.currentThread().getName() + "\n");
try {
Thread.sleep(10000);
} catch (Exception ex) {
System.out.print(ex.getMessage());
}

}
}

posted on 2018-08-11 18:18  暖风的风  阅读(508)  评论(0编辑  收藏  举报

导航