在使用Java作为开发语言时,如果需要高效率、并行地处理请求,通常采用的方法就是“多线程”。 我们可以简单地认为多个线程是同时在运行的,这样效率自然就会很高,那是不是同时开启的线程越多,效率越高呢。不是这样的,因为所有这些线程都是需要一个调度中心来保证协调地工作的,就好比有时候“人多反而误事”。所以说,是不是要采用多线程,以及要同时开启多少个线程都是要根据实际需要来定的,不是越多越好。 那多少个线程数才是合适的呢,最笨也最好的办法就是通过测试结果来定。但是,由于多个线程是要争夺CPU资源来实现并行的,这样我建议可以使用CPU数量的两倍来定。
在Java中要实现一个线程,可以通过继承java.lang.Thread类或者是实现java.lang.Runnable接口。其中的run方法就是线程的实现逻辑,调用start方法来启动线程。 下面的例子是继承了Thread类: [codesyntax lang="java"]
/**
* @author suren
* @date 2015-4-1
*
* http://surenpi.com
*/
public class Test
{
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
SuRenThread threadA = new SuRenThread();
SuRenThread threadB = new SuRenThread();
threadA.start();
threadB.start();
}
}
class SuRenThread extends Thread
{
@Override
public void run()
{
System.out.println(System.currentTimeMillis());
}
}
[/codesyntax] 下面的例子是实现了Runnable接口: [codesyntax lang="java"]
/**
* @author suren
* @date 2015-4-1
*
* http://surenpi.com
*/
public class Test
{
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
Thread threadA = new Thread(new SuRenRunnable());
Thread threadB = new Thread(new SuRenRunnable());
threadA.start();
threadB.start();
}
}
class SuRenRunnable implements Runnable
{
@Override
public void run()
{
System.out.println(System.currentTimeMillis());
}
}
[/codesyntax] 上述两种实现线程的方式都可以使用,但是由于Java规定类只能单继承,所以要使用继承Thread类的方式的话,就无法继承别的类了,这是他们的一个区别。
基本上只要使用了多线程,就会不可避免地要处理线程之间读写公共数据的安全性问题。使用线程间同步的方法是解决安全问题的一个手段。
java.util.concurrent.ExecutorService是JRE中提供的用于实现线程池的一个比较重要的接口。 下面是一个最简单的线程池的例子: [codesyntax lang="java"]
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author suren
* @date 2015-4-1
*
* http://surenpi.com
*/
public class Test
{
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
ExecutorService service = Executors.newCachedThreadPool();
service.execute(new SuRenRunnable());
service.execute(new SuRenRunnable());
service.execute(new SuRenRunnable());
}
}
class SuRenRunnable implements Runnable
{
@Override
public void run()
{
System.out.println(System.currentTimeMillis());
}
}
[/codesyntax]