从我在Java中使用线程的时间开始,我发现了这两种编写线程的方法:
用implements Runnable:
public class MyRunnable implements Runnable {
public void run() {
//Code
}
}
//Started with a "new Thread(new MyRunnable()).start()" call
Run Code Online (Sandbox Code Playgroud)
或者,用extends Thread:
public class MyThread extends Thread {
public MyThread() {
super("MyThread");
}
public void run() {
//Code
}
}
//Started with a "new MyThread().start()" call
Run Code Online (Sandbox Code Playgroud)
这两个代码块有什么显着差异吗?
我刚读完这篇文章:Java-5 ThreadPoolExecutor相对于Java-7 ForkJoinPool有什么优势?觉得答案不够直接.
您能用简单的语言和示例来解释,Java 7的Fork-Join框架与旧解决方案之间的权衡取舍是什么?
我还阅读了Google关于Java提示的#1热门提示:何时从javaworld.com 使用ForkJoinPool vs ExecutorService但文章没有回答标题问题时,它主要讨论api差异......
使用ExecutorService过度运行的线程Runnable进入Thread构造函数有什么好处?
我正在尝试创建多个线程,其数量取决于命令行的输入.我知道扩展Thread并不是最好的OO练习,除非你正在制作Thread的专用版本,但假设这个代码创建了所需的结果?
class MyThread extends Thread {
public MyThread (String s) {
super(s);
}
public void run() {
System.out.println("Run: "+ getName());
}
}
class TestThread {
public static void main (String arg[]) {
Scanner input = new Scanner(System.in);
System.out.println("Please input the number of Threads you want to create: ");
int n = input.nextInt();
System.out.println("You selected " + n + " Threads");
for (int x=0; x<n; x++)
{
MyThread temp= new MyThread("Thread #" + x);
temp.start();
System.out.println("Started Thread:" + x);
}
} …Run Code Online (Sandbox Code Playgroud) 我的问题是:使用它是否有意义Executors.newFixedThreadPool(1)??.在两个线程(main + oneAnotherThread)场景中,使用执行程序服务是否有效?是否通过调用new Runnable(){ }比使用ExecutorService更好地直接创建新线程?在这种情况下使用ExecutorService有什么好处和缺点?
PS:主线程和oneAnotherThread不访问任何公共资源.
我经历过:使用ExecutorService有什么好处?.并且一次只有一个线程!
除了Executor接口比普通线程(例如管理)具有一些优势之外,在执行以下操作之间是否存在任何真正的内部差异(大的性能差异,资源消耗......)
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(runnable);
Run Code Online (Sandbox Code Playgroud)
和:
Thread thread = new Thread(runnable);
thread.start();
Run Code Online (Sandbox Code Playgroud)
我这里只询问一个帖子.
我是java中这个并发编程的新手,并提出了以下场景,我在混淆时使用它.
场景1:在下面的代码中,我试图通过在GPSService类上调用.start ()来运行线程,这是一个Runnable实现.
int clientNumber = 0;
ServerSocket listener = new ServerSocket(port);
while (true) {
new GPSService(listener.accept(), clientNumber++, serverUrl).start();
}
Run Code Online (Sandbox Code Playgroud)
场景2:在下面的代码中,我试图通过使用ExecutorService类来运行线程,如图所示
int clientNumber = 0;
ServerSocket listener = new ServerSocket(port);
while(true) {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new GPSService(listener.accept(), client++, serverUrl));
executor.shutdown();
while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
// Threads are still running
System.out.println("Thread is still running");
}
// All threads are completed
System.out.println("\nThread completed it's execution and terminated successfully\n");
}
Run Code Online (Sandbox Code Playgroud)
我的问题是
在并发编程中调用线程的最佳实践是什么?
当我使用第一个或第二个时,我会得到什么结果(麻烦)?
注意:我一直面临着第一个场景的问题,即每隔几天该程序就会被吊死.那么,当我使用第一种方法时,该问题是否与预期有关.
任何好的/有用的答案将不胜感激:)谢谢
这是否可以以确定的方式运行多线程Java应用程序?我的意思是在我的应用程序的两个不同运行中始终使用相同的线程切换.
原因是在每次运行中以完全相同的条件运行模拟.
类似的情况是当使用随机数生成器获得总是相同的"随机"序列时,给出一些任意种子.
java ×8
concurrency ×3
command-line ×1
executor ×1
fork-join ×1
implements ×1
java-threads ×1
loops ×1
runnable ×1