在Java中,您必须有一个具有线程将访问的共享变量的类吗?

发布于 2024-11-07 21:56:53 字数 396 浏览 0 评论 0原文

我正在学习线程,但了解的东西不多。

我发现我需要实现 Runnable 接口并创建同一类的各种实例,以便每个线程执行每个实例。这是正确的吗?

如果正确,我需要创建另一个类来包含所有线程将访问/共享的变量?

编辑:我需要维护一些变量来协调线程工作,否则它们将执行相同的工作。这将是所有线程共享的一个变量。

编辑2:这个问题与此相关:如何使每个查询中带有 LIMIT 的 SQL 查询结果不同? .我需要维护已对数据库进行查询以设置 OFFSET 参数的线程数量。

I'm learning threads yet, but don't know much things.

I see that I need implement the Runnable interface and create various instances of the same class to each thread execute each one. It's correct?

If is correct, I need to create another class to contains the variables that will be accessed/shared by all threads?

EDIT: I need maintain some variables to coordinate the thread work, otherwise they will execute the same work. This will be one variable shared by all threads.

EDIT 2: this questions is related to this: How I make result of SQL querys with LIMIT different in each query? . I will need maintain the quantity of threads that have done a query to database to set the OFFSET parameter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉薄对峙 2024-11-14 21:56:53

是的,每个线程都需要一个 Runnable 实例来完成其工作。在某些情况下,线程可以共享同一个实例,但前提是实例中不存在需要在线程之间有所不同的状态。 通常您会希望每个线程中有不同的实例。

线程应该共享尽可能少的状态以避免出现问题,但如果您确实想共享状态,一般来说,您需要一个或多个实例来保存该状态,这是正确的。

请注意,此共享状态也可以保存在类变量而不是实例变量中。

Each thread needs an instance of a Runnable to do its work, yes. In some cases the threads could share the same instance, but only if there is no state held within the instance that needs to differ between threads. Generally you will want different instances in each thread.

Threads should share as little state as possible to avoid problems, but if you do want to share state, in general you are right that you will need an instance or instances somewhere to hold that state.

Note that this shared state could also be held in class variables rather than instance variables.

听风吹 2024-11-14 21:56:53

有很多方法可以解决这个问题......这实际上是一个关于设计模式的问题。

每个线程都可以通过其构造函数提供一个或多个描述其独特工作的对象。

或者,您可以为线程提供对工作队列的引用,线程可以从中查询下一个可用任务。

或者您可以在实现 Runnable 的类中放置一个方法,该方法可以由主线程调用...

有很多方法可以给这只猫换皮...我确信存在用于线程工作分配、配置等的现有库。

There are many ways to solve this...this is really a question about Design Patterns.

Each thread could be provided via it's constructor an object or objects that describe its unique work.

Or you could provide the thread with a reference to a work queue from which they could query the next available task.

Or you could put a method in the class that implements Runnable that could be called by a master thread...

Many ways to skin this cat...I'm sure there are existing libraries for thread work distribution, configuration, etc.

很快妥协 2024-11-14 21:56:53

让我们把所有的东西都放回原位吧。
语句new Thread(r)创建线程。但这个线程仍然没有运行。如果你说“

Thread t = new Thread(r);
t.start();

你让线程运行,即执行你的可运行的 run() 方法。

创建和运行线程的其他(相同)方法是继承 Thread 类并覆盖其 run() 的默认实现 。

现在,如果您有特定的逻辑并且希望在不同的线程中同时运行相同的逻辑,则必须创建不同的线程并执行它们的 start() 方法
如果您更喜欢实现 Runnable 接口并且您的逻辑不需要任何参数,您甚至可以仅创建一个 Runnable 实现实例并将其运行到不同的线程中。

public class MyLogic implements Runnable {
    public void run() {
        // do something.
    }
}


//// ................


Runnable r = new MyLogic();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);

t1.start();
t2.start();

现在,这一逻辑在 2 个独立的线程中同时运行,而我们只创建了一个 MyLogic 实例。

但是,如果您的逻辑需要参数,您应该创建单独的实例。

public class MyLogic implements Runnable {
    private int p;
    public MyLogic(int p) {
         this.p = p;
    }
    public void run() {
        // this logic uses value of p.
    }
}


//// ................


Thread t1 = new Thread(new MyLogic(111));
Thread t2 = new Thread(new MyLogic(222));

t1.start();
t2.start();

这 2 个线程使用不同的参数(111 和 222)运行相同的逻辑。

顺便说一句,这个示例展示了如何将值传递给线程。要从中获取信息,您应该使用类似的方法。定义成员变量result。该变量将由 run() 方法启动。提供适当的吸气剂。现在您可以将结果从线程传递给任何对此感兴趣的人。

显然,上面描述的是基础知识。我没有说任何关于同步、线程池、执行器等的内容。但我希望这能帮助你开始。然后找一些java线程教程看一遍。几天后,您将成为 Java 线程领域的世界级专家。 :)

快乐穿线。

Let's put all things on their places.
Statement new Thread(r) creates thread. But this thread still does not run. If you say"

Thread t = new Thread(r);
t.start();

you make thread to run, i.e. execute run() method of your runnable.

Other (equal) way to create and run thread is to inherit from class Thread and override default implementation of its run() method.

Now. If you have specific logic and you wish to run the same logic simultaneously in different threads you have to create different threads and execute their start() method.
If you prefer to implement Runnable interface and your logic does not require any parameters you even can create only one instance of your runnable implementation and run it into different threads.

public class MyLogic implements Runnable {
    public void run() {
        // do something.
    }
}


//// ................


Runnable r = new MyLogic();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);

t1.start();
t2.start();

Now this logic is running simultaniusly in 2 separate threads while we created only one instance of MyLogic.

If howerver your logic requires parameters you should create separate instances.

public class MyLogic implements Runnable {
    private int p;
    public MyLogic(int p) {
         this.p = p;
    }
    public void run() {
        // this logic uses value of p.
    }
}


//// ................


Thread t1 = new Thread(new MyLogic(111));
Thread t2 = new Thread(new MyLogic(222));

t1.start();
t2.start();

These 2 threads run the same logic with different arguments (111 and 222).

BTW this example shows how to pass values to thread. To get information from it you should use similar method. Define member variable result. The variable will be initiated by method run(). Provide appropriate getter. Now you can pass result from thread to anyone that is interesting to do this.

Obviously described above are basics. I did not say anything about synchronization, thread pools, executors etc. But I hope this will help you to start. Then find some java thread tutorial and go through it. In couple of days you will be the world class specialist in java threads. :)

Happy threading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文