简体   繁体   English

在Java中对线程使用method()

[英]Using methods() with threads in Java

Here's my problem: 这是我的问题:

I have a whole bunch of identical objects. 我有一堆相同的对象。 These objects interface with a server. 这些对象与服务器接口。 It takes 4 or 5 seconds to get data from the server, as the server is slow. 由于服务器速度较慢,因此需要4到5秒钟才能从服务器获取数据。

Now i need all the objects to get data. 现在,我需要所有对象来获取数据。 So i call MyObject.getData() for each object. 所以我为每个对象调用MyObject.getData()。 I could do it in a series but 20 objects, each taking 5 seconds is too slow. 我可以按系列进行,但是20个对象,每个对象花费5秒钟太慢。 I thought I should use threads and have each object on its own thread. 我以为我应该使用线程,并将每个对象放在自己的线程上。

Here's my question: 这是我的问题:

If i make the objects extend thread. 如果我使对象扩展线程。 Will a call to o MyObject.getData(); 将调用o MyObject.getData(); run in that object's thread, or in the thread the method was called from? 运行在该对象的线程中,还是在调用该方法的线程中运行? I know i can use Thread.Run() to get the object going but thats not what i want. 我知道我可以使用Thread.Run()来使对象运行,但这不是我想要的。 I want to get methods running at my will. 我想让方法随意运行。

So how do i do this? 那我该怎么做呢?

Thanks so much. 非常感谢。

The text book way to do this could be something like this: 教科书上的方法可能是这样的:

class GetDataObj implements Callable<Data> {
    public Data call(){
        //get data
        return data; 
    }
}

then 然后

ExecutorService exec = Executors.newCachedThreadPool();
Set<Callable<Data>> objects = //get objects;
List<Future<Data>> futures = exec.invokeAll(objects);
for(Future<Data>> future : futures){
    Data data = future.get();
    //do stuff with data
}
exec.shutdown();

Note that when you iterate through futures , the get() method will block until the result is available for that DataObj . 请注意,当您通过迭代futuresget()方法将阻塞,直到其结果是可用于DataObj If you want to wait until all data are available, this is fine. 如果您想等到所有数据都可用,就可以了。

If you call object.myMethod(), the method will run in the caller thread. 如果调用object.myMethod(),则该方法将在调用者线程中运行。

You have to start() the thread to make it run, not call the run() method. 您必须启动线程才能使其运行,而不是调用run()方法。

A think you can do, is rewriting your object so that the myMethod() method launch a new thread. 您可以做的就是重写对象,以便myMethod()方法启动一个新线程。 So you can use your objects exactly the same as actually. 因此,您可以使用与实际完全相同的对象。 But if myMethod return something, that change, because you have to wait for the thread to terminate befor 但是,如果myMethod返回某事,则该更改,因为您必须等待线程终止之前,

I think it would be best to use a thread pool to get data from the objects. 我认为最好使用线程池从对象中获取数据。 Therefore you would need each object to implement Runnable. 因此,您将需要每个对象来实现Runnable。 See thread pools . 请参阅线程池

If you pass a reference to a queue to the objects once they have got the data they can place it in the queue. 如果在对象获得数据后将对队列的引用传递给它们,则可以将其放入队列中。 The main thread can then just take the data off the queue when it is ready. 然后,主线程可以在就绪后将数据从队列中移出。 See the producer-consumer pattern . 参见生产者-消费者模式

An example of this is: 例如:

BlockingQueue<Data> queue = new BlockingQueue<Data>();
ExecutorService pool = Executors.newFixedThreadPool(5);

//implements Runnable, getting data from this
//places Data object in queue instead of returning it
DataObject obj = new DataObject(queue); 
pool.execute(obj); //invokes the run method of the DataObject
Data data = queue.take();

You would need to have a for-loop for more than one object. 您需要为多个对象建立一个for循环。

Hope this helps. 希望这可以帮助。

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

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