题目如下:

子线程循环10次,接着主线程循环100,接着又回到子线程循环10次, 接着再回到主线程又循环100,如此循环50次

思路如下:

子线程语主线程为互斥,可用SYNCHRONIZED。很容易想到如下代码

package concurrent;  

public class theFirstIdea{  

    /**
* @param args
*/
public static void main(String[] args) { new Thread(//子线程
new Runnable(){
public void run(){
for(int i=1;i<=50;i++){
synchronized(theFirstIdea.class){
for(int j=1;j<=10;j++){
System.out.println("sub thread: "+i+",loop: "+j);
}
}
}
}
}
).start(); new Thread(//主线程
new Runnable(){
public void run(){
for(int i=1;i<=50;i++){
synchronized(theFirstIdea.class){
for(int j=1;j<=100;j++){
System.out.println("main thread: "+i+",loop: "+j);
}
}
}
}
}
).start();
} }

由于运行结果很长(有5500行),所以在Eclipse 编译器无法全部看到,或看到的并不是最终运行结果。所以可以在Run -- Run configuration -- Common --勾选File,点击File System.选择到你想保存运行结果的地方,比如桌面,命名为1.txt. 此时桌面将会生产一个名为1.txt的文件,再次运行程序后,运行结果将保存到此文件中。便于查看。

查看后发现,基本达到要求,但并没有交替执行子线程和主线程。

而且上述代码不好,没有体现Java 的高类聚性,最好能将共同数据或共同方法归为同一类,即编写一个类来存放两个线程,便于修改。代码如下

package concurrent;  

public class theFirstIdea{  

    /**
* @param args
*/
public static void main(String[] args) { final MyThread threads=new MyThread();
new Thread(//子线程
new Runnable(){
public void run(){
for(int i=1;i<=50;i++){
threads.subThread(i);
}
}
}
).start(); for(int i=1;i<=50;i++){
threads.mainThread(i);
}
}
} class MyThread{
public synchronized void subThread(int i){
for(int j=1;j<=10;j++){
System.out.println("sub thread: "+i+",loop: "+j);
}
}
public synchronized void mainThread(int i){
for(int j=1;j<=10;j++){
System.out.println("main thread: "+i+",loop: "+j);
}
}
}

要让他们交替进行,可用信号量控制,并用wait  ,notify 进行线程间通信。易得

//子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,
//接着再回到主线程又循环100,如此循环50次,请写出程序。 public class ThreadTest{ public static void main(String[] args) { final MyThread threads=new MyThread();
new Thread(
new Runnable(){
public void run(){
for(int i=1;i<=50;i++){
threads.subThread(i);
}
}
}
).start();
new Thread(new Runnable(){
public void run(){
for(int i=1;i<=50;i++){
threads.mainThread(i);
}
}
}).start();
}
} class MyThread{
boolean bShouldSub=true;//标志子线程方法是否被调用
public synchronized void subThread(int i){
if(!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 :"+i+",loop : "+j);
}
bShouldSub=false;//子线程运行完毕
this.notify();//唤醒其他线程,即主线程
}
public synchronized void mainThread(int i){
if(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 :"+i+",loop : "+j);
}
bShouldSub=true;//主线程调用完毕
this.notify();//唤醒子线程
}
}

转自:http://blog.csdn.net/carlosli/article/details/8738960

【Java面试题】30 子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次,请写出程序。的更多相关文章

  1. 如果有三个Bool型变量,请写出一程序得知其中有2个以上变量的值是true

    下面这篇文章是从StackOverflow来的.LZ面试的时候遇到了一道面试题:“如果有三个Bool型变量,请写出一程序得知其中有2个以上变量的值是true”,于是LZ做了下面的这样的程序: bool ...

  2. 请写出一段JavaScript代码,要求页面有一个按钮,点击按钮弹出确认框。程序可以判断出用

    请写出一段JavaScript代码,要求页面有一个按钮,点击按钮弹出确认框.程序可以判断出用 户点击的是“确认”还是“取消”. 解答: <HTML> <HEAD> <TI ...

  3. 设计四个线程,当中共两个线程每次对j添加1,另外两个线程每次对j降低1。循环100次,写出程序。

    package cn.usst.DataTest6; /** * 设计四个线程,当中共两个线程每次对j添加1,另外两个线程每次对j降低1.循环100次,写出程序. * @ * */ public cl ...

  4. 设计四个线程,其中两个线程每次对j增加1,另外两个线程对j每次减1,写出程序

    /* * 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1.写出程序. */ public class ThreadTest { private int j; public sta ...

  5. 请写出正则表达式(regex),取得下列黄色部分的字符串 TEL: 02-236-9655/9659 FAX:02-236-9654 (黄色部分即02-236-9655/9659 ) ( 测试面试题)

    请写出正则表达式(regex),取得下列黄色部分的字符串 TEL: 02-236-9655/9659 FAX:02-236-9654 答: package test1; import java.uti ...

  6. 前端一面/面试常考题1-页面布局:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应。

    题目:假设高度已知,请写出三栏布局,其中左栏.右栏宽度各为300px,中间自适应. [题外话:日常宣读我的目标===想要成为一名优雅的程序媛] 一.分析 1. 题目真的像我们想得这么简单吗? 其实不然 ...

  7. C# - 习题02_写出程序的输出结果a.Fun()

    时间:2017-08-23 整理:byzqy 题目:写出程序的输出结果: 文件:Program.cs 1 using System; 2 3 namespace Interview1 4 { 5 pu ...

  8. C# - 习题01_写出程序的输出结果a.Fun2(b)、b.Fun2(a)

    时间:2017-08-23 整理:byzqy 题目:请写出下列程式的结果: 文件:A.cs 1 using System; 2 3 namespace InterView 4 { 5 public c ...

  9. 请写出5种常见到的runtime exception。

    请写出5种常见到的runtime exception. 解答: NullPointerException:当操作一个空引用时会出现此错误. NumberFormatException:数据格式转换出现 ...

随机推荐

  1. 集群瓶颈:磁盘IO必读

    首先需要知道什么是IO: IO是输入输出接口阅读本文章可以带着下面问题1.集群的瓶颈为什么IO?2.你对IO了解多少? 这里面只说个人观点:当我们面临集群作战的时候,我们所希望的是即读即得.可是面对大 ...

  2. 每日英语:He Diets, She Diets: More Weight-Loss Plans Target Men

    Weight-loss companies are becoming savvier about getting men to go on a diet. savvier:更加精明 Men and w ...

  3. pre 强制换行

    当前位置:懒人建站 > javascript > 网页特效 > css强制pre标签换行 css强制pre标签换行,pre标签的功能就是保留文本源格式,将会保留文本的长度,空格 空行 ...

  4. 使用jquery插件validate制作的表单验证案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. FreeRTOS 任务栈大小确定及其溢出检测

    以下转载自安富莱电子: http://forum.armfly.com/forum.php FreeRTOS 的任务栈设置不管是裸机编程还是 RTOS 编程,栈的分配大小都非常重要. 局部变量,函数调 ...

  6. php memcached在windows上的使用

    php的memcached是比memcache,效率更高的memcache缓存扩展. 然而windows下并没有这个扩展,于是做单元测试时要把代码上传到linux服务器,再运行,甚是麻烦. (当然另外 ...

  7. python 调用函数 / 类型转换 / 切片/ 迭代

    调用函数 / 类型转换 /  切片/ 迭代 1. 调用函数:abs(),max(),min() 2. 数据类型转换:int(),float(),str(),tool(),a=abs, 3. 定义函数, ...

  8. Unity3D可以查看YAML格式的场景文件,采用Notepad++

    在Editor Settings 将Asset Serialization 的 mode设置成Force Text,否则不能查看YAML格式! Unity圣典描述:Textual Scene File ...

  9. CKFinder 弹出窗口操作并设置回调函数

    CKFinder 弹出窗口操作并设置回调函数 官方例子参考CKFinderJava-2.4.1/ckfinder/_samples/popup.html 写一个与EXT集成的小例子 Ext.defin ...

  10. Android——数据存储:手机外部存储 SD卡存储

    xml <EditText android:layout_width="match_parent" android:layout_height="wrap_cont ...