parent
bfa6c34d27
commit
c3bad6627d
@ -1,36 +0,0 @@
|
|||||||
1.使用synchronized关键字同步线程
|
|
||||||
package step2;
|
|
||||||
|
|
||||||
public class Task {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
|
||||||
final insertData insert = new insertData();
|
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
new Thread(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
insert.insert(Thread.currentThread());
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class insertData {
|
|
||||||
|
|
||||||
public static int num = 0;
|
|
||||||
|
|
||||||
/********* Begin *********/
|
|
||||||
public synchronized void insert(Thread thread) {
|
|
||||||
|
|
||||||
for (int i = 0; i <= 5; i++) {
|
|
||||||
num++;
|
|
||||||
System.out.println(num);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/********* End *********/
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in new issue