50,549
社区成员
发帖
与我相关
我的任务
分享
package thread.worker;
import org.junit.Test;
/**
* Created by hu on 2017/8/17.
*/
public class CountWorker {
protected int sum = 1;
@Test
public void testWorker() {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new CountThread(this));
thread.setName("huli" + i);
thread.start();
}
System.out.println("sum = " + sum);
}
}
package thread.worker;
/**
* Created by hu on 2017/8/17.
*/
public class CountThread implements Runnable {
private CountWorker countWorker;
public CountThread(CountWorker countWorker) {
this.countWorker = countWorker;
}
@Override
public void run() {
while (countWorker.sum < 20) {
System.out.println("sum is : " + countWorker.sum);
countWorker.sum++;
}
}
}
sum = 20
sum = 21
sum = 23