开发者

In Java, you must have a class with shared variables that threads will access?

开发者 https://www.devze.com 2023-03-05 17:40 出处:网络
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 correc

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 differe开发者_运维技巧nt in each query? . I will need maintain the quantity of threads that have done a query to database to set the OFFSET parameter.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消