我有两个选择来执行程序(结果是我需要Out_str)。
选项1:
Out_str = "";
for (i = 1; i <= 20000; i++) {
Out_str = Out_str + "word";
}
选项2:
Runnable runnable1 = new Runnable() {
public void run() {
Out_str1 = "";
for (i1 = 1; i1 <= 10000; i1++) {
Out_str1 = Out_str1 + "word";
}
}
};
Runnable runnable2 = new Runnable() {
public void run() {
Out_str2 = "";
for (i2 = 1; i2 <= 10000; i2++) {
Out_str2 = Out_str2 + "word";
}
}
};
Thread thread1 = new Thread(runnable1);
thread1.start();
Thread thread2 = new Thread(runnable2);
thread2.start();
为什么使用线程不会使我的程序执行得更快(在两种情况下,使用笔记本电脑执行该程序大约需要12秒)?在这种情况下,我应该使用什么(以便更快地获得Out_str)?在这种情况下,使用服务会有所帮助吗?
Thread
允许您同时运行进程。但是,您还没有获得任何处理能力,在同等优先级下,我猜想线程将以循环方式运行。