三道多线程编程题_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 三道多线程编程题

三道多线程编程题

 2013/10/14 15:30:48  MouseLearnJava  程序员俱乐部  我要评论(0)
  • 摘要:本文记录三道多线程编程题。第一道:有四个线程1、2、3、4。线程1的功能就是输出A,线程2的功能就是输出B,以此类推.........现在有四个文件file1,file2,file3,file4。初始都为空。现要让四个文件呈如下格式:file1:ABCDAB....file2:BCDABC....file3:CDABCD....file4:DABCDA....程序如下:packagemy.test;importjava.io.FileWriter;importjava.io
  • 标签:多线程 编程 线程

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

第一道:
有四个线程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....

程序如下:
class="java">package my.test;

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

public class FileWriteUtil {

	private String filename = "A.txt";

	private char value = 'A';

	private int threadNo = 1;

	public FileWriteUtil() {
		super();
	}

	/**
	 * @param filename
	 * @param value
	 */
	public FileWriteUtil(String filename, char value) {
		super();
		this.filename = filename;
		this.value = value;
	}

	public void write() {
		FileWriter writer = null;
		try {
			writer = new FileWriter(filename, true);
			writer.write(String.valueOf(value));
			writer.flush();
		} 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;
			}
		}

		generateNext();
		nextThreadNo();
	}

	public void rebuildFilename() {
		if (threadNo == 1) {
			filename = "A.txt";
		} else if (threadNo == 2) {
			filename = "B.txt";
		} else if (threadNo == 3) {
			filename = "C.txt";
		} else if (threadNo == 4) {
			filename = "D.txt";
		}
	}

	/**
	 * @return the threadNo
	 */
	public int getThreadNo() {
		return threadNo;
	}

	/**
	 * @param threadNo
	 *            the threadNo to set
	 */
	public void setThreadNo(int threadNo) {
		this.threadNo = threadNo;
	}

	public void nextThreadNo() {
		if (threadNo == 4) {
			threadNo = 1;
		} else {
			threadNo++;
		}
	}

	public void generateNext() {
		if (value == 'D') {
			value = 'A';
		} else {
			value += 1;
		}

	}

	/**
	 * @return the filename
	 */
	public String getFilename() {
		return filename;
	}

	/**
	 * @param filename
	 *            the filename to set
	 */
	public void setFilename(String filename) {
		this.filename = filename;
	}

	/**
	 * @return the value
	 */
	public char getValue() {
		return value;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(char value) {
		this.value = value;
	}

}


package my.test;

public class WriteRunnable implements Runnable {

	private FileWriteUtil util = null;

	private int threadNo = 1;

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

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

					if (threadNo == 1) {
						util.setValue(c);
					}

					util.write();
					util.rebuildFilename();
					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));
		service.execute(new WriteRunnable(util, 2));
		service.execute(new WriteRunnable(util, 3));
		service.execute(new WriteRunnable(util, 4));

		service.shutdown();
		
	}

}


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


第三道


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

http://mouselearnjava.iteye.com/blog/1949228
发表评论
用户名: 匿名