一个线程通信问题(sub运行10次,然后main运行100次,这样交替循环50次)
2017-02-10 17:46
225 查看
/**
* sub运行10次,然后main运行100次,这样交替循环50次
* @author Administrator
*
*/
public class TraditionalThreadCommunication {
/**
* @param args
*/
public static void main(String[] args) {
final Business business = new Business();
new Thread(
new Runnable() {
@Override
public void run() {
for(int i=1;i<=50;i++){
business.sub(i);
}
}
}
).start();
new Thread(new Runnable() {
@Override
public void run() {
for(int i=1;i<=50;i++){
business.main(i);
}
}
}).start();
}
}
class Business {
private boolean bShouldSub = true;
public synchronized void sub(int i){
while(!bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int j=1;j<=10;j++){
System.out.println("sub thread sequence of " + j + ",loop of " + i);
}
bShouldSub = false;
this.notify();
}
public synchronized void main(int i){
while(bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int j=1;j<=100;j++){
System.out.println("main thread sequence of " + j + ",loop of " + i);
}
bShouldSub = true;
this.notify();
}
}
* sub运行10次,然后main运行100次,这样交替循环50次
* @author Administrator
*
*/
public class TraditionalThreadCommunication {
/**
* @param args
*/
public static void main(String[] args) {
final Business business = new Business();
new Thread(
new Runnable() {
@Override
public void run() {
for(int i=1;i<=50;i++){
business.sub(i);
}
}
}
).start();
new Thread(new Runnable() {
@Override
public void run() {
for(int i=1;i<=50;i++){
business.main(i);
}
}
}).start();
}
}
class Business {
private boolean bShouldSub = true;
public synchronized void sub(int i){
while(!bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int j=1;j<=10;j++){
System.out.println("sub thread sequence of " + j + ",loop of " + i);
}
bShouldSub = false;
this.notify();
}
public synchronized void main(int i){
while(bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int j=1;j<=100;j++){
System.out.println("main thread sequence of " + j + ",loop of " + i);
}
bShouldSub = true;
this.notify();
}
}
相关文章推荐
- 多线程面试题:子线程循环10次,接着主线程循环100次;接着回到子线程循环10次,然后再回到主线程循环100次,如此交替50次。
- 传统线程通信(子线程运行10次,主线程运行100次,如此循环50次)
- java一道多线程题,子线程循环10次,主线程接着循环100次,如此循环50次的问题
- 子线程循环10次 主线程循环100次 然后如此反复50次
- 子程序循环10次,接着主程序循环100次,然后子程序又循环10次,主程序循环100次,这样循环50次
- 子线程运行10次后,主线程运行100次,子主交叉运行。如此循环50次。
- 第十天 :让一线程执行10次,然后让二线程执行10,然后一执行,二执行,这样循环50次,编写程序
- 线程通信(子线程运行10次,主线程运行100次,如此循环50次)[用condition替代wait,notify等待和唤醒操作]
- 主线程和子线程均循环10次,两者交替运行50次
- 子线程循环10次,紧接着主线程循环100次,来回50次
- (13)多线程与并发库之java5阻塞队列(BlockingQueue)的应用----子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程循环100次,如此循环50次
- 子线程循环10次,主线程执行100次,紧接着子线程执行10次,再执行主线程 如此往复50次。子线程循环的时候主线程不能打扰,相同的是主线程执行循环的时候子线程也不能打扰
- (13)多线程与并发库之java5阻塞队列(BlockingQueue)的应用----子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程循环100次,如此循环50次
- 线程同步通信技术(四)子线程10次与主线程100次来回循环执行50次
- 面试题:子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程循环100次,如此循环50次,请写出程序
- 并发包实现:机试题-〉子线程循环10次,接着回到主线程循环100次,又接着回到子线程循环10次,再接着回到主线程又循环100次,如次循环50次,请写出程序
- java多线程: 子线程循环10次,主线程接着循环100次,如此循环50次
- 机试题-〉子线程循环10次,接着回到主线程循环100次,又接着回到子线程循环10次,再接着回到主线程又循环100次,如次循环50次,请写出程序
- 【线程高新】——【面试题】子线程运行10次,主线程运行100次,轮换运行50次
- 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程100次,如此循环50次