3 回答
TA贡献1824条经验 获得超5个赞
这不仅会复杂得多,而且会慢很多倍。但是,作为练习,我建议使用最简单的方法,即使用 parallelStream
String upper = "abcdef".chars().parallel()
.map(Character::toUpperCase)
.mapToObj(c -> Character.toString((char) c))
.collect(Collectors.joining());
System.out.println(upper);
你可能想知道,为什么这么慢?从人的尺度考虑这个,你可以
a) 在纸上手工转换字母,或
b) 你可以给三个朋友寄三封信,让他们每人给你寄两封大写的信。
除了更糟糕的是,因为必须首先启动线程,这需要更长的时间,所以真的是这样
c) 找三个愿意回信的新朋友然后 b)
TA贡献1890条经验 获得超9个赞
注意:String str = "abcdef".toUpperCase();对于短字符串,仅使用要快得多(在我的计算机上,当字符串长度约为 50000 个字符时,多线程代码开始工作得更快)。
该字符串被拆分为一个字符数组 ( chars)。每个线程遍历数组chars,将一个字母转换为大写,并跳过两个字母供其他线程处理。当所有线程都完成后,数组将变回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条经验 获得超2个赞
是的,问题在我第一次回复后被修改,现在有一个可以接受的答案,但我还是想发表评论。这可能被认为是一个更完整的示例,因为CapitalizeWorker线程正在从CapitalizeJob类中获取工作并将结果返回。我认为这是一个更好的例子,因为它不依赖于特定数量的线程,而是针对您的系统拥有的核心数量进行了优化。它也在以干净的方式关闭并等待结果。只是我价值 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);
}
}
添加回答
举报