我们假设我有很多对象:
public class Main {
public static DB d1 = new DB(1);
public static DB d2 = new DB(2);
public static DB d3 = new DB(3);
public static DB d4 = new DB(4);
我想修改它们。
public static void main(String[] args) {
d1.modifyObject();
d2.modifyObject();
d3.modifyObject();
d4.modifyObject();
}
}
和我希望它们能够同时修改,因为它需要一些时间。看起来我需要多线程。
这是我的DB
课程:
import java.awt.EventQueue;
import java.util.Date;
import java.util.Random;
public class DB {
private int id = 0;
private long field = 0;
public void DB(int id) {
this.id = id;
}
// The contents of this method are not very important.
private void modifyField() {
// [some database interactions which take some seconds to execute]
// for simplicity, emulated with sleep:
long newValue = 0;
try {
newValue = (this.id + new Date().getTime()) % 42;
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.field = newValue;
}
public void modifyObject() {
Runnable r = new Runnable(){ @Override public void run() {
this.modifyField(); // THIS DOES NOT WORK OF COURSE, I can't access the objects content from a thread
}};
EventQueue.invokeLater(r);
}
}
我希望在不延迟主线程的情况下更改Main.d1
,Main.d2
,Main.d3
,Main.d4
等内容。我曾经通过访问Main.d1
本身中的DB
来实现此目的。这显然只有在我只有一个对象时才有效。现在,由于我必须处理多个对象,因此我无法再静态访问Main
个对象。
我的问题很简单,但我担心没有简单的答案:我有什么选择将this.modifyField();
放入自己的线程中?
答案 0 :(得分:1)
我认为你错了this
。
下面:
Runnable r = new Runnable(){ @Override public void run() {
this.modifyField(); // THIS DOES NOT WORK OF COURSE, I can't access the objects content from a thread
}};
this
指的是new Runnable
。您可以这样访问封闭的DB
对象:
Runnable r = new Runnable(){ @Override public void run() {
DB.this.modifyField();
}};
此外,如果您使用的是Java 8,则可以使用方法引用更简洁地编写更简洁的内容:
Runnable r = this::modifyField;
答案 1 :(得分:1)
使用ExecutorServce:
public void foo() throws InterruptedException {
final ExecutorService executorService = newFixedThreadPool(4); //find what works best for you, setting the number of threads as the number of tasks will not be the best solution in all cases
final Future<?> runD1Modify = executorService.submit(getModifyObjectRunnable(d1));
final Future<?> runD2Modify = executorService.submit(getModifyObjectRunnable(d2));
final Future<?> runD3Modify = executorService.submit(getModifyObjectRunnable(d3));
final Future<?> runD4Modify = executorService.submit(getModifyObjectRunnable(d4));
// or in java8
final Future<?> runD1Modify = executorService.submit(() -> d1.modifyField());
final Future<?> runD1Modify = executorService.submit(() -> d2.modifyField());
final Future<?> runD1Modify = executorService.submit(() -> d3.modifyField());
final Future<?> runD1Modify = executorService.submit(() -> d4.modifyField());
}
private Runnable getModifyObjectRunnable(final DB db) {
return new Runnable() {
@Override
public void run() {
db.modifyField();
}
};
}