java多线程编译,求代码! 5
WriteathreadAtokeepreadingonesentenceonceatimefromthekeyboard,boththreadsBandCareable...
Write a thread A to keep reading one sentence once a time from the keyboard, both threads B and C are able to handle this sentence, thread B should write this sentence into a text file, thread C should display this sentence on the screen, thread B takes care of the inputting sentence whose first letter is uppercase, otherwise thread C does it.
展开
展开全部
public class Main {
private static ArrayBlockingQueue<String> file_q = new ArrayBlockingQueue<String>(1000);
private static ArrayBlockingQueue<String> console_q = new ArrayBlockingQueue<String>(1000);
public static void main(String[] args) {
new Thread(new Thread_A()).start();
new Thread(new Thread_B()).start();
new Thread(new Thread_C()).start();
}
static class Thread_A implements Runnable {
public void run() {
BufferedReader thread_a = new BufferedReader(new InputStreamReader(System.in));
String inputString;
while (true) {
try {
inputString = thread_a.readLine();
char firstChar = inputString.charAt(0);
if (firstChar >= 'A' && firstChar <= 'Z') {
file_q.put(inputString);//大写,放入写文件队列
} else {
console_q.put(inputString);//非大写,放入写控制台队列
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 文件输出线程
*/
static class Thread_B implements Runnable {
public void run() {
while (true) {
try {
String to_write_file_str = file_q.poll(300L, TimeUnit.MILLISECONDS);
if (null != to_write_file_str && to_write_file_str.trim().length() > 0) {
RandomAccessFile randomFile = new RandomAccessFile("E:/test.txt", "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.writeBytes(String.format("%s\r\n", to_write_file_str));
randomFile.close();
System.out.println("Thread B writed!");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 控制台输出线程
*/
static class Thread_C implements Runnable {
public void run() {
while (true) {
try {
String to_write_console_str = console_q.poll(300L, TimeUnit.MILLISECONDS);
if (null != to_write_console_str && to_write_console_str.trim().length() > 0)
System.out.printf("Thread C say: %s%n", to_write_console_str);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
private static ArrayBlockingQueue<String> file_q = new ArrayBlockingQueue<String>(1000);
private static ArrayBlockingQueue<String> console_q = new ArrayBlockingQueue<String>(1000);
public static void main(String[] args) {
new Thread(new Thread_A()).start();
new Thread(new Thread_B()).start();
new Thread(new Thread_C()).start();
}
static class Thread_A implements Runnable {
public void run() {
BufferedReader thread_a = new BufferedReader(new InputStreamReader(System.in));
String inputString;
while (true) {
try {
inputString = thread_a.readLine();
char firstChar = inputString.charAt(0);
if (firstChar >= 'A' && firstChar <= 'Z') {
file_q.put(inputString);//大写,放入写文件队列
} else {
console_q.put(inputString);//非大写,放入写控制台队列
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 文件输出线程
*/
static class Thread_B implements Runnable {
public void run() {
while (true) {
try {
String to_write_file_str = file_q.poll(300L, TimeUnit.MILLISECONDS);
if (null != to_write_file_str && to_write_file_str.trim().length() > 0) {
RandomAccessFile randomFile = new RandomAccessFile("E:/test.txt", "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.writeBytes(String.format("%s\r\n", to_write_file_str));
randomFile.close();
System.out.println("Thread B writed!");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 控制台输出线程
*/
static class Thread_C implements Runnable {
public void run() {
while (true) {
try {
String to_write_console_str = console_q.poll(300L, TimeUnit.MILLISECONDS);
if (null != to_write_console_str && to_write_console_str.trim().length() > 0)
System.out.printf("Thread C say: %s%n", to_write_console_str);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询