我有一个字符串:“abcdef”和 3 个线程:t1、t2 和 t3。我希望他们返回大写的字符串:“ABCDEF”(字符顺序无关紧要)。我设法将字符串大写 3 次。我希望这一切只发生一次。这是主类:
public static void main(String[] args) {
Thread t1 = new Thread(new CapitalizeString("Thread 1", "abcdef"));
Thread t2 = new Thread(new CapitalizeString("Thread 2", "abcdef"));
Thread t3 = new Thread(new CapitalizeString("Thread 3", "abcdef"));
t1.start();
t2.start();
t3.start();
}
这是用于大写的类:
import java.util.Random;
public class CapitalizeString implements Runnable {
String list;
String nameThread;
public CapitalizeString(String name, String str) {
nameThread = name;
list = str;
}
public void capitalize() {
char[] arr = list.toCharArray();
for (int i = 0; i < list.length(); i++) {
arr[i] = Character.toUpperCase(arr[i]);
System.out.println("Thread-ul " + nameThread + " solved " + arr[i]);
}
System.out.print("\n");
}
@Override
public void run() {
try {
capitalize();
} catch (Exception e) {
}
}
}
沧海一幻觉
当年话下
慕婉清6462132
相关分类