`
MouseLearnJava
  • 浏览: 467158 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

三道多线程编程题

阅读更多
本文记录三道多线程编程题。

第一道:
有四个线程1、2、3、4。线程1的功能就是输出A线程2的功能就是输出B,以此类推......... 现在有四个文件file1,file2,file3, file4。初始都为空。
现要让四个文件呈如下格式:
file1:A B C D A B....
file2:B C D A B C....
file3:C D A B C D....
file4:D A B C D A....

程序如下:
package my.test;

import java.io.FileWriter;
import java.io.IOException;

public class FileWriteUtil {

	private int currentThreadNum = 1;

	/** 记录将字符写入文件的次数 */
	private int count = 0;
	
	private int currentFileNum = 1;
	
	public void write(char value, int threadNum) {
		
		
		generateNextFileNum();
		
		FileWriter writer = null;
		try {
			writer = new FileWriter("file" + currentFileNum + ".txt", true);
			writer.write(String.valueOf(value));
			System.out.printf(
					"ThreadNum=%d is executing. %c is written into file file%d.txt \n",
					currentThreadNum, value, currentFileNum);
			writer.flush();
			count++;
			currentThreadNum = threadNum;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (null != writer) {
			try {
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				writer = null;
			}
		}

		generateNextThreadNum();
		
	}

	public int getCurrentThreadNum() {
		return currentThreadNum;
	}

	public void setCurrentThreadNum(int currentThreadNum) {
		this.currentThreadNum = currentThreadNum;
	}
	
	private void generateNextFileNum()
	{
		if (count % 4 == 0) {
			currentFileNum = 1;
		}else
		{
			currentFileNum++;
		}
	}

	private void generateNextThreadNum() {

		if (count % 4 == 0) {
			if (currentThreadNum < 3) {
				currentThreadNum += 2;
			} else {
				currentThreadNum = (currentThreadNum + 2) % 4;
			}
		}else
		{
			if(currentThreadNum == 4)
			{
				currentThreadNum=1;
			}else
			{
				currentThreadNum++;
			}
		}
	}

}


package my.test;

public class WriteRunnable implements Runnable {

	private FileWriteUtil util = null;

	private int threadNum = 1;

	private char value = ' ';

	/**
	 * @param util
	 */
	public WriteRunnable(FileWriteUtil util, int threadNum, char value) {
		super();
		this.util = util;
		this.threadNum = threadNum;
		this.value = value;
	}

	public void run() {
		/**
		 * 假设循环16次, 一直循环可以使用while(true) 或者 for(;;)
		 */
		for (int i = 0; i < 16; i++) {
			synchronized (util) {
				while (threadNum != util.getCurrentThreadNum()) {
					try {
						util.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				util.write(value, threadNum);
				
				util.notifyAll();

			}
		}

	}

}


package my.test;  
  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
  
/** 
 * 有四个线程1、2、3、4。
 * 
 * 线程1的功能就是输出A,线程2的功能就是输出B,
 * 
 * 以此类推......... 现在有四个文件file1, 
 * file2,file3, file4。初始都为空。现要让四个文件呈如下格式: 
 * file1:A B C D A B.... 
 * file2:B C D A B C.... 
 * file3:C D A B C D.... 
 * file4:D A B C D A.... 
 * 
 * @author Eric 
 * 
 */  
public class Main {  
      
    public static void main(String[] args) {  
        FileWriteUtil util = new FileWriteUtil();  
          
  
        ExecutorService service = Executors.newCachedThreadPool();  
  
        service.execute(new WriteRunnable(util, 1, 'A'));  
        service.execute(new WriteRunnable(util, 2, 'B'));  
        service.execute(new WriteRunnable(util, 3, 'C'));  
        service.execute(new WriteRunnable(util, 4, 'D'));  
  
        service.shutdown();  
          
    }  
  
}  


程序运行结果:
控制台输出--->
ThreadNum=1 is executing. A is written into file file1.txt
ThreadNum=2 is executing. B is written into file file2.txt
ThreadNum=3 is executing. C is written into file file3.txt
ThreadNum=4 is executing. D is written into file file4.txt
ThreadNum=2 is executing. B is written into file file1.txt
ThreadNum=3 is executing. C is written into file file2.txt
ThreadNum=4 is executing. D is written into file file3.txt
ThreadNum=1 is executing. A is written into file file4.txt
ThreadNum=3 is executing. C is written into file file1.txt
ThreadNum=4 is executing. D is written into file file2.txt
ThreadNum=1 is executing. A is written into file file3.txt
ThreadNum=2 is executing. B is written into file file4.txt
ThreadNum=4 is executing. D is written into file file1.txt
ThreadNum=1 is executing. A is written into file file2.txt
ThreadNum=2 is executing. B is written into file file3.txt
ThreadNum=3 is executing. C is written into file file4.txt
ThreadNum=1 is executing. A is written into file file1.txt
ThreadNum=2 is executing. B is written into file file2.txt
ThreadNum=3 is executing. C is written into file file3.txt
ThreadNum=4 is executing. D is written into file file4.txt
ThreadNum=2 is executing. B is written into file file1.txt
ThreadNum=3 is executing. C is written into file file2.txt
ThreadNum=4 is executing. D is written into file file3.txt
ThreadNum=1 is executing. A is written into file file4.txt
ThreadNum=3 is executing. C is written into file file1.txt
ThreadNum=4 is executing. D is written into file file2.txt
ThreadNum=1 is executing. A is written into file file3.txt
ThreadNum=2 is executing. B is written into file file4.txt
ThreadNum=4 is executing. D is written into file file1.txt
ThreadNum=1 is executing. A is written into file file2.txt
ThreadNum=2 is executing. B is written into file file3.txt
ThreadNum=3 is executing. C is written into file file4.txt
ThreadNum=1 is executing. A is written into file file1.txt
ThreadNum=2 is executing. B is written into file file2.txt
ThreadNum=3 is executing. C is written into file file3.txt
ThreadNum=4 is executing. D is written into file file4.txt
ThreadNum=2 is executing. B is written into file file1.txt
ThreadNum=3 is executing. C is written into file file2.txt
ThreadNum=4 is executing. D is written into file file3.txt
ThreadNum=1 is executing. A is written into file file4.txt
ThreadNum=3 is executing. C is written into file file1.txt
ThreadNum=4 is executing. D is written into file file2.txt
ThreadNum=1 is executing. A is written into file file3.txt
ThreadNum=2 is executing. B is written into file file4.txt
ThreadNum=4 is executing. D is written into file file1.txt
ThreadNum=1 is executing. A is written into file file2.txt
ThreadNum=2 is executing. B is written into file file3.txt
ThreadNum=3 is executing. C is written into file file4.txt
ThreadNum=1 is executing. A is written into file file1.txt
ThreadNum=2 is executing. B is written into file file2.txt
ThreadNum=3 is executing. C is written into file file3.txt
ThreadNum=4 is executing. D is written into file file4.txt
ThreadNum=2 is executing. B is written into file file1.txt
ThreadNum=3 is executing. C is written into file file2.txt
ThreadNum=4 is executing. D is written into file file3.txt
ThreadNum=1 is executing. A is written into file file4.txt
ThreadNum=3 is executing. C is written into file file1.txt
ThreadNum=4 is executing. D is written into file file2.txt
ThreadNum=1 is executing. A is written into file file3.txt
ThreadNum=2 is executing. B is written into file file4.txt
ThreadNum=4 is executing. D is written into file file1.txt
ThreadNum=1 is executing. A is written into file file2.txt
ThreadNum=2 is executing. B is written into file file3.txt
ThreadNum=3 is executing. C is written into file file4.txt


file1文件中的内容-->
ABCDABCDABCDABCD

file2文件中的内容:-->
BCDABCDABCDABCDA

file3文件中的内容:-->
CDABCDABCDABCDAB

file4文件中的内容:-->
DABCDABCDABCDABC

第二道:
一个线程打印 1~52,另一个线程打印字母A-Z。打印顺序为12A34B56C……5152Z。
http://mouselearnjava.iteye.com/blog/1949693



第三道


有三个线程分别打印A、B、C,请用多线程编程实现,在屏幕上循环打印10次ABCABC…

http://mouselearnjava.iteye.com/blog/1949228
2
2
分享到:
评论
4 楼 MouseLearnJava 2013-10-16  
dujiayi001 写道
弱弱的问一句,我跑了你的代码,但是出来结果是:
pool-1-thread-1 : A.txt=>A
pool-1-thread-2 : B.txt=>B
pool-1-thread-3 : C.txt=>C
pool-1-thread-4 : D.txt=>D
pool-1-thread-1 : A.txt=>B
pool-1-thread-2 : B.txt=>C
pool-1-thread-3 : C.txt=>D
pool-1-thread-4 : D.txt=>A
pool-1-thread-1 : A.txt=>C
pool-1-thread-2 : B.txt=>D
pool-1-thread-3 : C.txt=>A
pool-1-thread-4 : D.txt=>B

跟题目问的好像不对劲,题目要求是线程1的功能就是输出A,线程2的功能就是输出B,而你的结果是线程1只输出A文件


谢谢你的指正,确实跑题了。

程序已经修正。

一个运行结果如下:
ThreadNum=1 is executing. A is written into file file1.txt
ThreadNum=2 is executing. B is written into file file2.txt
ThreadNum=3 is executing. C is written into file file3.txt
ThreadNum=4 is executing. D is written into file file4.txt
ThreadNum=2 is executing. B is written into file file1.txt
ThreadNum=3 is executing. C is written into file file2.txt
ThreadNum=4 is executing. D is written into file file3.txt
ThreadNum=1 is executing. A is written into file file4.txt
ThreadNum=3 is executing. C is written into file file1.txt
ThreadNum=4 is executing. D is written into file file2.txt
ThreadNum=1 is executing. A is written into file file3.txt
ThreadNum=2 is executing. B is written into file file4.txt
ThreadNum=4 is executing. D is written into file file1.txt
ThreadNum=1 is executing. A is written into file file2.txt
ThreadNum=2 is executing. B is written into file file3.txt
ThreadNum=3 is executing. C is written into file file4.txt
ThreadNum=1 is executing. A is written into file file1.txt
ThreadNum=2 is executing. B is written into file file2.txt
ThreadNum=3 is executing. C is written into file file3.txt
ThreadNum=4 is executing. D is written into file file4.txt
ThreadNum=2 is executing. B is written into file file1.txt
ThreadNum=3 is executing. C is written into file file2.txt
ThreadNum=4 is executing. D is written into file file3.txt
ThreadNum=1 is executing. A is written into file file4.txt
ThreadNum=3 is executing. C is written into file file1.txt
ThreadNum=4 is executing. D is written into file file2.txt
ThreadNum=1 is executing. A is written into file file3.txt
ThreadNum=2 is executing. B is written into file file4.txt
ThreadNum=4 is executing. D is written into file file1.txt
ThreadNum=1 is executing. A is written into file file2.txt
ThreadNum=2 is executing. B is written into file file3.txt
ThreadNum=3 is executing. C is written into file file4.txt
ThreadNum=1 is executing. A is written into file file1.txt
ThreadNum=2 is executing. B is written into file file2.txt
ThreadNum=3 is executing. C is written into file file3.txt
ThreadNum=4 is executing. D is written into file file4.txt
ThreadNum=2 is executing. B is written into file file1.txt
ThreadNum=3 is executing. C is written into file file2.txt
ThreadNum=4 is executing. D is written into file file3.txt
ThreadNum=1 is executing. A is written into file file4.txt
ThreadNum=3 is executing. C is written into file file1.txt
ThreadNum=4 is executing. D is written into file file2.txt
ThreadNum=1 is executing. A is written into file file3.txt
ThreadNum=2 is executing. B is written into file file4.txt
ThreadNum=4 is executing. D is written into file file1.txt
ThreadNum=1 is executing. A is written into file file2.txt
ThreadNum=2 is executing. B is written into file file3.txt
ThreadNum=3 is executing. C is written into file file4.txt
ThreadNum=1 is executing. A is written into file file1.txt
ThreadNum=2 is executing. B is written into file file2.txt
ThreadNum=3 is executing. C is written into file file3.txt
ThreadNum=4 is executing. D is written into file file4.txt
ThreadNum=2 is executing. B is written into file file1.txt
ThreadNum=3 is executing. C is written into file file2.txt
ThreadNum=4 is executing. D is written into file file3.txt
ThreadNum=1 is executing. A is written into file file4.txt
ThreadNum=3 is executing. C is written into file file1.txt
ThreadNum=4 is executing. D is written into file file2.txt
ThreadNum=1 is executing. A is written into file file3.txt
ThreadNum=2 is executing. B is written into file file4.txt
ThreadNum=4 is executing. D is written into file file1.txt
ThreadNum=1 is executing. A is written into file file2.txt
ThreadNum=2 is executing. B is written into file file3.txt
ThreadNum=3 is executing. C is written into file file4.txt
3 楼 dujiayi001 2013-10-15  
弱弱的问一句,我跑了你的代码,但是出来结果是:
pool-1-thread-1 : A.txt=>A
pool-1-thread-2 : B.txt=>B
pool-1-thread-3 : C.txt=>C
pool-1-thread-4 : D.txt=>D
pool-1-thread-1 : A.txt=>B
pool-1-thread-2 : B.txt=>C
pool-1-thread-3 : C.txt=>D
pool-1-thread-4 : D.txt=>A
pool-1-thread-1 : A.txt=>C
pool-1-thread-2 : B.txt=>D
pool-1-thread-3 : C.txt=>A
pool-1-thread-4 : D.txt=>B

跟题目问的好像不对劲,题目要求是线程1的功能就是输出A,线程2的功能就是输出B,而你的结果是线程1只输出A文件
2 楼 MouseLearnJava 2013-10-14  
cywhoyi 写道
挺好练习,第一题我采用的是
	
        private static Semaphore A = new Semaphore(1);
	private static Semaphore B = new Semaphore(1);
	private static Semaphore C = new Semaphore(1);
	private static Semaphore D = new Semaphore(1);
	private static AtomicInteger value = new AtomicInteger(1);

信号量+原子类的操作,感觉比LZ更为简洁,顶下面试而用


是的。JDK 1.5 之后的J.U.C 包对多线程编程是个很不撮的选择,方便很多。
1 楼 cywhoyi 2013-10-14  
挺好练习,第一题我采用的是
	
        private static Semaphore A = new Semaphore(1);
	private static Semaphore B = new Semaphore(1);
	private static Semaphore C = new Semaphore(1);
	private static Semaphore D = new Semaphore(1);
	private static AtomicInteger value = new AtomicInteger(1);

信号量+原子类的操作,感觉比LZ更为简洁,顶下面试而用

相关推荐

    史上最全60道多线程面试题答案

    多线程并发编程是Java编程中重要的一块内容,也是面试重点覆盖区域。所以,学好多线程并发编程对Java程序员来来说极其重要的。

    施工高处作业安全技术规范方案(jgj80~2016年)考试题(卷)库单选50道多选20道判断30道.doc

    施工高处作业安全技术规范方案(jgj80~2016年)考试题(卷)库单选50道多选20道判断30道.doc

    《货币银行学》300道题【含答案】

    6. **商业银行经营方针**:在商业银行的经营方针中,“**安全性**”、“**流动性**”和“**盈利性**”被认为是三个基本原则。其中,安全性是基础,流动性是条件,而盈利性是目标。 7. **基础货币的影响因素**:在...

    Java-recitations.rar_多选题

    这可能包括类和对象的高级用法、继承与多态性的实际应用、接口设计、异常处理机制、集合框架的深入理解、Java IO流的灵活运用、多线程编程的技巧,以及Java的高级特性如反射、注解和Lambda表达式等。这些问题要求...

    PMP模拟练习题(一)180题.pdf

    这份模拟试题不仅包含了170道单选题和10道多选题,而且全面覆盖了项目管理的五个过程组——启动、计划、执行、监控和控制、关闭。通过这些题目的练习,可以检验和提高项目管理专业人员的知识和技能。 在项目的启动...

    Java-review-questions.rar_40_多选题

    最后的2道编程题,可能是设计并实现一个小型的功能模块或者解决一个实际问题。这类题目旨在检验复习者的实际编程能力和问题解决能力,这在实际工作中尤为重要。 总结来说,这份复习资料全面覆盖了Java编程的各个...

    2021届高三数学新高考“8+4+4”小题狂练(3)(解析).doc

    第三题涉及到函数的奇偶性及图像识别,通过排除法确定函数的图形。这需要考生理解函数的基本性质和图像特征。 4. 定义域的求解 第四题要求确定函数的定义域,这需要考生解决不等式组来找到函数能够取值的区间。 ...

    ks_8.rar_多选题_题库练习

    例如,描述中提到的两道多选题,第一题是单选题,只有一项正确答案;第二题是多选题,要求选择两个正确选项。 在设计多选题时,IT专家通常会遵循以下原则: 1. **难度适中**:确保题目的难度适合目标群体,避免过于...

    网络试题(网络技术)

    精心制作:其中包含九十道单选题、二十道多选题、九十道填空题、十道简答题。包含各种难点、易错点

    2017年江苏省中学综合素质文化素养:重要的作家作品考试题.pdf

    5. **制约课程改革的因素**:第五题中提到的三大因素——社会、知识、儿童,是影响课程改革的重要条件,课程设计需考虑社会需求、学科知识的更新以及学生的成长需求。 6. **教师的观察力**:第七题提到的教师具备的...

    2021年湖北武汉市住房保障管理中心招考聘用冲刺模拟试题.doc

    12. 管理授权:第十三道单选题描述了一种授权方式——柔性授权,允许下属在一定范围内自主决策。 13. 心理学:第十四道判断题指出团体凝聚力与从众行为的关系,凝聚力强的团队更易出现从众行为。 14. 法律权利:第...

    贵州省黔东南苗族侗族自治州榕江县通用知识真题汇总2008年-2018年高频考点版(一).docx

    13. **三次产业划分**:第十三道单选题关于产业分类,划分三次产业的依据主要是生产要素的密集程度。 14. **经济体制改革**:第十四道单选题提到党的十八大强调健全现代市场体系。 15. **不告不理原则**:第十五...

    全国ITAT教育工程就业技能大赛比赛说明

    复赛则转为编程题,包含4道操作题和2道附加题,满分150分,考试时间为3小时,侧重考察Java语言的实际操作能力。决赛依然为编程题,但难度升级,两道题目分别检验选手解决问题和创新思维的能力,满分为120分,考试...

    .net 实现简单在线考试系统

    可以实现单选题、多选题维护、填空题、判断题的在线维护和管理,添加生成试卷后可以自动从题库进行调用试题,考试完成后自动打分,后台可以进行统计。 考试科目 考试科目添加 考试科目维护 用户信息 用户添加 用户...

    广东省深圳实验承翰学校2020-2021学年高一下学期数学周测(三) Word版含答案.docx

    13. **复数的实部与代数运算**:填空题第十三题要求解复数的实部,这需要通过复数的代数运算来实现。 14. **向量的线性组合与坐标运算**:填空题第十四题可能涉及到向量的线性组合,通过坐标运算求解。 15. **向量...

    银行从业资格考试科目及题型题量.pdf

    1. 《法律法规与综合能力》:初级考试有90道单选题(每题0.5分,共45分)、40道多选题(每题1分,共40分)和15道判断题(每题1分,共15分)。 2. 《个人理财》:初级考试结构与《法律法规与综合能力》相同。 3. ...

    2023年演出经纪人题库

    500道单选题+200道多选题

Global site tag (gtag.js) - Google Analytics