【发布时间】:2015-04-04 04:33:58
【问题描述】:
我已经开始学习多核编程和开发并行算法。这可以通过在 Java 中使用多线程轻松完成。于是,我创建了两个文本文件,10行内容如下:
This is the first line in file 1
This is the second line in file 1
This is the third line in file 1
This is the fourth line in file 1
This is the fifth line in file 1
This is the sixth line in file 1
This is the seventh line in file 1
This is the eighth line in file 1
This is the ninth line in file 1
This is the tenth line in file 1
同样,在另一个文本文件中,文件 1 被替换为 文件 2。我写了一个程序来读取文件的内容,有和没有线程。它们如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class SimpleThread {
static void printFile(BufferedReader br) throws Exception
{
for(String line; (line = br.readLine())!=null; )
System.out.println(line);
}
public static void main(String args[]) throws Exception
{
double startTime = System.nanoTime();
BufferedReader br1 = new BufferedReader(new FileReader(new File("test1.txt")));
BufferedReader br2 = new BufferedReader(new FileReader(new File("test2.txt")));
SimpleThread.printFile(br1);
SimpleThread.printFile(br2);
System.out.println(System.nanoTime() - startTime + "ns");
}
}
使用多线程的程序如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Threading extends Thread{
BufferedReader br;
public Threading(String fileName)
{
try{
br = new BufferedReader(new FileReader(new File(fileName)));
start();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
private void printFile(BufferedReader br) throws Exception
{
for(String line; (line = br.readLine())!=null; )
System.out.println(line);
}
public void run()
{
try{
printFile(br);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void main(String args[]) throws Exception
{
double startTime = System.nanoTime();
Threading t1 = new Threading("test1.txt");
Threading t2 = new Threading("test2.txt");
System.out.println(System.nanoTime() - startTime + "ns");
}
}
现在,当我比较两个程序的执行时间时,我发现单线程程序需要 1544589.0ns,而多线程程序需要 410522.0ns。
我很想知道提高速度的因素。我发现它大约是 0.23。
修改了使用多线程的代码后,发现单线程程序执行速度更快,这在更大程度上增加了我的困惑。
这是修改后的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Threading extends Thread{
BufferedReader br;
public Threading(String fileName)
{
try{
br = new BufferedReader(new FileReader(new File(fileName)));
start();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
private void printFile(BufferedReader br) throws Exception
{
for(String line; (line = br.readLine())!=null; )
System.out.println(line);
}
public void run()
{
try{
printFile(br);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void main(String args[]) throws Exception
{
double startTime = System.nanoTime();
Threading t1 = new Threading("test1.txt");
Threading t2 = new Threading("test2.txt");
t1.join(); //waiting for t1 to finish
t2.join(); //waiting for t2 to finish
System.out.println(System.nanoTime() - startTime + "ns");
}
}
现在执行时间是:
单线程 - 1459052.0ns
多线程 - 1768651.0ns
为什么系统的行为不自然?
现在,我的问题是:
- 会增加线程数,减少执行时间吗?
- 何时应该在编写程序时使用多线程
- 是否可以将相同的文件概念移植到数据库中,每个线程根据类别读取数据库的一部分,例如新闻、体育、政治等信息将由相应的线程读取,最终结果将被捆绑在一起。这可行吗?
- 是否应该仅将多线程用于 CPU 密集型程序?
【问题讨论】:
-
您的多线程程序不会等到工作线程完成其工作后才报告运行时。
-
视情况而定,有很多优点和缺点,所以我认为你应该阅读一些关于多线程的书。
标签: java multithreading