简体   繁体   English

Java多线程 - 最佳实践

[英]Java Multithreading - Best practice

I'm having trouble at understanding out teachers' method of multithreading. 我很难理解教师的多线程方法。 Well, before showing both our examples I must say he's teaching a bunch of newbies (including me I guess ^.^) so he might have picked a method of multithreading that's easy to understand. 好吧,在展示我们的两个例子之前,我必须说他正在教一些新手(包括我,我想^。^)所以他可能选择了一种易于理解的多线程方法。 Also, I'm not visiting his classes, but I've got his script which says the following: 另外,我没有参观他的课程,但是我的脚本上写着以下内容:

He's doing multithreading like this: 他正在做这样的多线程:

class MyThread implements Runnable {

Thread t;

MyThread() {

    t = new Thread(this, "Demo");
    t.start();
}

public void run() {

    try {

        for (int i = 5; i > 0; i--) {

            System.out.println("Child-Thread:" + i);
            Thread.sleep(1000);
        }
    } catch (InterruptedException e) {
        System.out.println("Child interrupted");
    }
    System.out.println("Child finished");
}

}

I find it cleaner doing like this: 我觉得这样做比较干净:

public class Aufg1 {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    MyThread t1 = new MyThread(1);

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
    executor.schedule(t1, 1, TimeUnit.SECONDS);
}

static class MyThread implements Runnable {

    int number;

    public MyThread(int number) {
        this.number = number;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (int i = 5; i > 0; i--) {
            System.out.println("Thread " + number + ": " + i);
        }
    }


}
}

Don't bother why I used a Thread-Pool of size 5. I needed it for another exercise. 不要理会我为什么使用大小为5的线程池。我需要它进行另一次练习。 Also, I could've used a ThreadPool for a single execution is this example. 此外,我可以使用ThreadPool进行单次执行就是这个例子。

Is there a big difference? 有很大的不同吗? Is there an even cleaner way? 有更清洁的方式吗? I know there're some other methods to multithread as well, though I only showed one in here. 我知道还有其他一些多线程的方法,虽然我只在这里展示了一个。

Thanks! 谢谢!

I would not name the runnable class something with thread in its name, it is a little confusing. 我不会将runnable类命名为其名称中的线程,这有点令人困惑。

Other than that, you are using the java concurrency package, he's using the lower level thread class that this package is built on. 除此之外,您使用的是java并发包,他正在使用构建此包的较低级别的线程类。 Probably because one must learn how to walk before one can learn how to run. 可能是因为在学会如何跑步之前必须学会走路。 :) :)

There are many implementations of threads, being the easiest one Thread itself, a basic implementation of runnable. 线程有很多实现,是最简单的Thread本身,是runnable的基本实现。 I guess his point is just to understand what a Thread is; 我想他的观点只是了解Thread是什么; in your career you will need threads as a tool and you need to know its inner workings, and then use the best implementation that suits you. 在你的职业生涯中,你需要线程作为工具,你需要知道它的内部工作,然后使用适合你的最佳实现。

Both executors and threads will create one or more (in the case of executors) An Executor is normally used instead of explicitly creating threads. 执行程序和线程都将创建一个或多个(在执行程序的情况下)通常使用Executor而不是显式创建线程。 * *

Anyway as personal advice I recommend you to keep focusing on new ways of using threads, as each implementation will be better for different problems. 无论如何,作为个人建议,我建议您继续关注使用线程的新方法,因为每个实现对于不同的问题都会更好。

Being somebody who's taught multithreading to beginners I have to admit that I have (at least once) been guilty of writing inelegant code in order to demonstrate a point. 作为一个为初学者教授多线程的人,我不得不承认,我(至少有一次)为编写不优雅的代码而犯了罪,以证明一个观点。

This example seems to be demonstrating what the sleep method does in an artificial way - which your code does not. 这个例子似乎是以一种人为的方式展示了睡眠方法的作用 - 而你的代码却没有。 Your teacher should probably have included some explanation with their code. 您的老师应该在他们的代码中包含一些解释。

Is there a big difference? 有很大的不同吗? Is there an even cleaner way? 有更清洁的方式吗? I know there're some other methods to multithread as well, though I only showed one in here. 我知道还有其他一些多线程的方法,虽然我只在这里展示了一个。

It depends on what you're trying to achieve. 这取决于你想要达到的目标。 As others have said, there's lots of different ways to do multithreading. 正如其他人所说,有许多不同的方法可以进行多线程处理。 The code will have slightly different behaviour, so there is a difference - but that doesn't mean that once is 'cleaner'. 代码的行为略有不同,因此存在差异 - 但这并不意味着曾经“更清洁”。 As this is an artificial example (counting to 5) we can't really say what the 'cleanest' or 'best' method is. 由于这是一个人为的例子(数到5),我们无法真正说出“最干净”或“最好”的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM