该程序的目标是将三个字符串发送到三个单独的线程,并让它们按字符逐个打印出每个字符串。我想在每个输入字符串之间留一个空行,但我不确定将每个线程置于等待状态的位置或停止它以使输出变化。大部分时间它都是字符串的组合混乱,有时一个分开并整齐地出来。
import java.util.Scanner;
public class Threader {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Hello");
String str = scan.nextLine();
String str2 = scan.nextLine();
String str3 = scan.nextLine();
Thread A = new Thread(new MyRunnable(str));
A.start();
Thread B = new Thread(new MyRunnable(str2));
B.start();
Thread C = new Thread(new MyRunnable(str3));
C.start();
}
}
public class MyRunnable implements Runnable {
String str, str2, str3;
public MyRunnable(String str){
this.str = str;
}
@Override
public void run() {
for (int i = 0; i < str.length(); i++){
System.out.println(str.charAt(i));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
这是一个答案,拼出并用它们之间的空格分隔每个字符串。有趣的是,我的老师只是希望他们同时开始并输出所有混乱只是为了展示竞争条件。 :p但是在这里,有几个循环与睡眠。丑陋但它有效
import java.util.Scanner;
public class Threader {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter 3 lines...Suuuuh dude");
/*
* Strangs
*/
String str = scan.nextLine();
String str2 = scan.nextLine();
String str3 = scan.nextLine();
/*
* Intantiate threads holding runnable objects
*/
Thread A = new Thread(new MyRunnable(str));
Thread B = new Thread(new MyRunnable(str2));
Thread C = new Thread(new MyRunnable(str3));
A.start();
// B.start();
// C.start();
/*
* try { A.join(); B.join(); C.join();
*
* } catch (InterruptedException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
/*
* Uncomment While() Loops To Eliminate race conditions (Threads will
* sleep if previous one is Alive(Won't fight for cpu))
*/
while (A.isAlive()) {
try {
B.sleep(1000);
System.out.println("\n");
} // end try
catch (InterruptedException e) {
e.printStackTrace();
} // end catch
} // end while
B.start();
while (B.isAlive()) {
try {
C.sleep(1000);
System.out.println("\n");
} // end try
catch (InterruptedException e) {
e.printStackTrace();
} // end catch
} // end while
C.start();
}// main METHOD
}// Threader CLASS
//具有Runnable Int。
的类public class MyRunnable implements Runnable {
String str, str2, str3;
public MyRunnable(String str) {
this.str = str;
this.str2 = str2;
this.str3 = str3;
}
@Override
public void run() {
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
try {
// sleep 1 ms before printing next char
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // for
System.out.println("end" + str);
}// run
}
答案 1 :(得分:-1)
这个怎么样?
import java.util.Scanner;
public class Threader {
public Threader(String str, String str2, String str3) {
Thread A = new Thread(new MyRunnable(str));
Thread B = new Thread(new MyRunnable(str2));
Thread C = new Thread(new MyRunnable(str3));
A.start();
B.start();
C.start();
}
public synchronized void printString(String str) {
for (Character c : str.toCharArray()) {
System.out.println(c);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Hello");
String str = scan.nextLine();
String str2 = scan.nextLine();
String str3 = scan.nextLine();
new Threader(str, str2, str3);
}
class MyRunnable implements Runnable {
private String str;
public MyRunnable(String str) {
this.str = str;
}
@Override
public void run() {
printString(str);
}
}
}