[英]How to use Instance variable in multiple swing worker thread with thread-safe?
I am using swing worker thread to communicate rest services. 我正在使用swing worker线程来传达其余服务。 My scenerio is i am calling one thread to get the data from rest services and add into my list variable .
我的场景是,我正在调用一个线程从其余服务获取数据并将其添加到我的list变量中。 and another one thread to push the list of data to save it.
另一个线程推送数据列表以保存它。 How to handle this scenerio with thread safe
如何使用线程安全处理此场景
My sample code is below 我的示例代码如下
private LinkedList<LinkInfo> ***linkInfoList*** = new LinkedList<FlowLinkEntry>();
SwingWorker<LinkInfo, Void> loadLinkInfoThread = new SwingWorker<LinkInfo, Void>() {
@Override
protected LinkInfo doInBackground() throws Exception {
InputStream is = new URL("Http://URL").openStream();
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is,
Charset.forName("UTF-8")));
LinkInfo linkInfo = (LinkInfo)JsonConverter
.fromJson(reader, LinkInfo.class);
***linkInfoList*** .add(linkInfo);
} finally {
is .close();
}
return linkInfo;
}
}
SwingWorker<Void, Void> saveLinkInfoThread = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
//post data to particular url
//linkInfoList data is posting in this thread
URL url = new URL(http://url);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(***linkInfoList*** );
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
}
} }
My questions are 我的问题是
How to store the data in linkInfoList as request order wise? 如何按请求顺序将数据存储在linkInfoList中? (ie) if i call load thread several time,the data should inserted in list request wise.
(即)如果我多次调用加载线程,则应该将数据插入列表请求中。
How to put wait state the save Thread if load Thread is already in progress. 如果加载线程已在进行中,如何使等待状态保存线程。 I mean that if load thread is in running condition, after completing load thread then only save thread should have to run
我的意思是,如果加载线程处于运行状态,则在完成加载线程之后,只应运行保存线程
I would synchronize list at initialization as Oracle says . 我会像Oracle所说的那样在初始化时同步列表。
List ***linkInfoList*** = Collections.synchronizedList(new LinkedList(...));
Then you would have to test the list if there are any items to save, otherwise wait. 然后,您必须测试列表是否有任何要保存的项目,否则请等待。
SwingWorker<Void, Void> saveLinkInfoThread = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
List info = new ArrayList();
while (***linkInfoList***.isEmpty()){
Thread.currentThread().sleep(1000);
}
while (!***linkInfoList***.isEmpty()){
info.add(***linkInfoList***.remove(0));
}
//post data to particular url
//linkInfoList data is posting in this thread
URL url = new URL(http://url);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(info);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.