什么时候使用线程,什么时候使用线程池?
When to use a Thread and when to use a Threadpool?
任何人都可以在这个示例中指导我 Thread 和 ThreadPool 它们之间有什么区别?哪个更好用...?缺点是什么?
我使用了一个线程池,为什么在这种情况下使用它 true 或 false ?
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
class ThreeThread implements Runnable {
String c;
Semaphore s1;
Semaphore s2;
public ThreeThread(String s, Semaphore s1, Semaphore s2) {
this.c = s;
this.s1 = s1;
this.s2 = s2;
}
@Override
public void run() {
while (true) {
try {
s1.acquire();
Thread.sleep(400);
System.out.println(c);
s2.release();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println(e);
}
}
}
public static void main(String[] args) {
Semaphore sm1 = new Semaphore(1);
Semaphore sm2 = new Semaphore(0);
Semaphore sm3 = new Semaphore(0);
ExecutorService es = Executors.newFixedThreadPool(3);
es.execute(new ThreeThread("1", sm1, sm2));
es.execute(new ThreeThread("2", sm2, sm3));
es.execute(new ThreeThread("3", sm3, sm1));
}
}
参见 ThreadPoolExecutor
的文档:
Thread pools address two different problems:
they usually provide
improved performance when executing large numbers of asynchronous
tasks, due to reduced per-task invocation overhead,
and they provide a
means of bounding and managing the resources, including threads,
consumed when executing a collection of tasks.
Each ThreadPoolExecutor
also maintains some basic statistics, such as the number of completed
tasks.
嗯,我认为 ThreadPoolExecutor 提供更好的性能,因为它管理线程池,最大限度地减少实例化新线程、分配内存的开销...
如果您要启动数千个线程,它会为您提供一些您必须自己编程的排队功能...
Threads & Executors 是不同的工具,用在不同的场景。。。在我看来,就像在问为什么可以用HashMap 还要用ArrayList?他们是不同的...
何时使用:
线程:我想自己管理线程创建,决定创建时间,监控,同步,线程清理。
执行者:我希望specialist/manager执行这些活动。
任何人都可以在这个示例中指导我 Thread 和 ThreadPool 它们之间有什么区别?哪个更好用...?缺点是什么?
我使用了一个线程池,为什么在这种情况下使用它 true 或 false ?
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
class ThreeThread implements Runnable {
String c;
Semaphore s1;
Semaphore s2;
public ThreeThread(String s, Semaphore s1, Semaphore s2) {
this.c = s;
this.s1 = s1;
this.s2 = s2;
}
@Override
public void run() {
while (true) {
try {
s1.acquire();
Thread.sleep(400);
System.out.println(c);
s2.release();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println(e);
}
}
}
public static void main(String[] args) {
Semaphore sm1 = new Semaphore(1);
Semaphore sm2 = new Semaphore(0);
Semaphore sm3 = new Semaphore(0);
ExecutorService es = Executors.newFixedThreadPool(3);
es.execute(new ThreeThread("1", sm1, sm2));
es.execute(new ThreeThread("2", sm2, sm3));
es.execute(new ThreeThread("3", sm3, sm1));
}
}
参见 ThreadPoolExecutor
的文档:
Thread pools address two different problems:
they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead,
and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks.
Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.
嗯,我认为 ThreadPoolExecutor 提供更好的性能,因为它管理线程池,最大限度地减少实例化新线程、分配内存的开销...
如果您要启动数千个线程,它会为您提供一些您必须自己编程的排队功能...
Threads & Executors 是不同的工具,用在不同的场景。。。在我看来,就像在问为什么可以用HashMap 还要用ArrayList?他们是不同的...
何时使用: 线程:我想自己管理线程创建,决定创建时间,监控,同步,线程清理。
执行者:我希望specialist/manager执行这些活动。