3 回答

TA貢獻1824條經(jīng)驗 獲得超5個贊
這不僅會復(fù)雜得多,而且會慢很多倍。但是,作為練習(xí),我建議使用最簡單的方法,即使用 parallelStream
String upper = "abcdef".chars().parallel()
.map(Character::toUpperCase)
.mapToObj(c -> Character.toString((char) c))
.collect(Collectors.joining());
System.out.println(upper);
你可能想知道,為什么這么慢?從人的尺度考慮這個,你可以
a) 在紙上手工轉(zhuǎn)換字母,或
b) 你可以給三個朋友寄三封信,讓他們每人給你寄兩封大寫的信。
除了更糟糕的是,因為必須首先啟動線程,這需要更長的時間,所以真的是這樣
c) 找三個愿意回信的新朋友然后 b)

TA貢獻1890條經(jīng)驗 獲得超9個贊
注意:String str = "abcdef".toUpperCase();對于短字符串,僅使用要快得多(在我的計算機上,當(dāng)字符串長度約為 50000 個字符時,多線程代碼開始工作得更快)。
該字符串被拆分為一個字符數(shù)組 ( chars)。每個線程遍歷數(shù)組chars,將一個字母轉(zhuǎn)換為大寫,并跳過兩個字母供其他線程處理。當(dāng)所有線程都完成后,數(shù)組將變回String.
public static void main(String args[]) {
String str = "abcdef";
System.out.println(str);
char[] chars = str.toCharArray();
Thread t1 = new Thread(new CapitalizeString(chars, 0, 3));
Thread t2 = new Thread(new CapitalizeString(chars, 1, 3));
Thread t3 = new Thread(new CapitalizeString(chars, 2, 3));
t1.start();
t2.start();
t3.start();
// wait until all threads finish their work
try{
t1.join();
t2.join();
t3.join();
}catch(InterruptedException e){ }
// print the result
str = String.valueOf(chars);
System.out.println(str);
}
類大寫字符串:
public class CapitalizeString implements Runnable {
char[] chars;
int start;
int numThreads;
public CapitalizeString(char[] chars, int startIndex, int numThreads) {
this.chars = chars;
this.numThreads = numThreads;
start = startIndex;
}
@Override
public void run() {
for(int x = start; x < chars.length; x += numThreads){
chars[x] = Character.toUpperCase(chars[x]);
}
}
}

TA貢獻1804條經(jīng)驗 獲得超2個贊
是的,問題在我第一次回復(fù)后被修改,現(xiàn)在有一個可以接受的答案,但我還是想發(fā)表評論。這可能被認為是一個更完整的示例,因為CapitalizeWorker線程正在從CapitalizeJob類中獲取工作并將結(jié)果返回。我認為這是一個更好的例子,因為它不依賴于特定數(shù)量的線程,而是針對您的系統(tǒng)擁有的核心數(shù)量進行了優(yōu)化。它也在以干凈的方式關(guān)閉并等待結(jié)果。只是我價值 0.02 美元。
private void run() throws InterruptedException {
CapitalizeJob capitalizeJob = new CapitalizeJob("abcdef");
int processors = Runtime.getRuntime().availableProcessors();
ExecutorService executors = Executors.newFixedThreadPool(processors);
for ( int t = 0; t < processors; ++t) {
executors.execute(new CapitalizeWorker(capitalizeJob));
}
executors.shutdown(); // this is missing from OP thread example
executors.awaitTermination(10, TimeUnit.SECONDS);
System.out.println( capitalizeJob.getResult() );
}
public class CapitalizeWorker implements Runnable {
private CapitalizeJob capitalizeJob;
CapitalizeWorker(CapitalizeJob capitalizeJob) {
this.capitalizeJob = capitalizeJob;
}
@Override
public void run() {
char c;
while ( (c = capitalizeJob.getNextChar()) != 0 ) {
System.out.println(Thread.currentThread().toString());
capitalizeJob.setNextChar(Character.toUpperCase(c));
}
}
}
public class CapitalizeJob {
private char[] arr;
private int jobIndex;
private char[] result;
private int resultIndex;
public CapitalizeJob(String name) {
arr = name.toCharArray();
result = new char[arr.length];
jobIndex = 0;
resultIndex = 0;
}
public synchronized char getNextChar() {
return jobIndex < arr.length ? arr[jobIndex++] : 0 ;
}
public synchronized void setNextChar(char c) {
result[resultIndex++] = c;
}
public String getResult() {
return new String(result);
}
}
添加回答
舉報