The ArrayList .add method does not work within a thread
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've tried to get a String from the request function and add it to the lastList ArrayList, but it will not be added normally.
In this case, how can I add?
As a global variable public ArrayList lastList = new ArrayList <> (); But it did not work.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Log.d(MainActivity.TAG, "Task runned.");
for (int i = 0; i < alarmList.size(); i++) {
final String str = alarmList.get(i).split("#!%~#");
for (int j = 0; j <= 8; j++) {
try {
final String output = AddActivity.request(str[1], str[2]);
handler.post(new Runnable() {
public void run() {
if (Integer.parseInt(str[3]) == Integer.parseInt(StringUtils.substringBetween(output, str[1] + " ", "화"))) {
lastList.add(output);
}
}
});
Log.d(MainActivity.TAG, "OK");
break;
} catch (Exception ex) {
Log.d(MainActivity.TAG, "Exception.");
}
}
}
}
});
thread.start();
try {
thread.join();
Log.d(MainActivity.TAG, "thread end.");
Log.d(MainActivity.TAG, "Size: " + lastList.size()); // Size: 0
}
I expect the output of Size: 1, but the actual output is Size: 0.
java android arraylist
add a comment |
I've tried to get a String from the request function and add it to the lastList ArrayList, but it will not be added normally.
In this case, how can I add?
As a global variable public ArrayList lastList = new ArrayList <> (); But it did not work.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Log.d(MainActivity.TAG, "Task runned.");
for (int i = 0; i < alarmList.size(); i++) {
final String str = alarmList.get(i).split("#!%~#");
for (int j = 0; j <= 8; j++) {
try {
final String output = AddActivity.request(str[1], str[2]);
handler.post(new Runnable() {
public void run() {
if (Integer.parseInt(str[3]) == Integer.parseInt(StringUtils.substringBetween(output, str[1] + " ", "화"))) {
lastList.add(output);
}
}
});
Log.d(MainActivity.TAG, "OK");
break;
} catch (Exception ex) {
Log.d(MainActivity.TAG, "Exception.");
}
}
}
}
});
thread.start();
try {
thread.join();
Log.d(MainActivity.TAG, "thread end.");
Log.d(MainActivity.TAG, "Size: " + lastList.size()); // Size: 0
}
I expect the output of Size: 1, but the actual output is Size: 0.
java android arraylist
Are you sure that thread started withhandler.post()
already finished when you callsize()
on your list?
– Ivan
Jan 4 at 0:41
Yes, I called size () after confirming that the thread was completed through the thread.join () function.
– a101a78
Jan 4 at 0:54
No, you are joining with one thread, put you are passing anotherRunnable
tohandler.post()
which I think will be executed in one more thread.
– Ivan
Jan 4 at 2:36
add a comment |
I've tried to get a String from the request function and add it to the lastList ArrayList, but it will not be added normally.
In this case, how can I add?
As a global variable public ArrayList lastList = new ArrayList <> (); But it did not work.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Log.d(MainActivity.TAG, "Task runned.");
for (int i = 0; i < alarmList.size(); i++) {
final String str = alarmList.get(i).split("#!%~#");
for (int j = 0; j <= 8; j++) {
try {
final String output = AddActivity.request(str[1], str[2]);
handler.post(new Runnable() {
public void run() {
if (Integer.parseInt(str[3]) == Integer.parseInt(StringUtils.substringBetween(output, str[1] + " ", "화"))) {
lastList.add(output);
}
}
});
Log.d(MainActivity.TAG, "OK");
break;
} catch (Exception ex) {
Log.d(MainActivity.TAG, "Exception.");
}
}
}
}
});
thread.start();
try {
thread.join();
Log.d(MainActivity.TAG, "thread end.");
Log.d(MainActivity.TAG, "Size: " + lastList.size()); // Size: 0
}
I expect the output of Size: 1, but the actual output is Size: 0.
java android arraylist
I've tried to get a String from the request function and add it to the lastList ArrayList, but it will not be added normally.
In this case, how can I add?
As a global variable public ArrayList lastList = new ArrayList <> (); But it did not work.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Log.d(MainActivity.TAG, "Task runned.");
for (int i = 0; i < alarmList.size(); i++) {
final String str = alarmList.get(i).split("#!%~#");
for (int j = 0; j <= 8; j++) {
try {
final String output = AddActivity.request(str[1], str[2]);
handler.post(new Runnable() {
public void run() {
if (Integer.parseInt(str[3]) == Integer.parseInt(StringUtils.substringBetween(output, str[1] + " ", "화"))) {
lastList.add(output);
}
}
});
Log.d(MainActivity.TAG, "OK");
break;
} catch (Exception ex) {
Log.d(MainActivity.TAG, "Exception.");
}
}
}
}
});
thread.start();
try {
thread.join();
Log.d(MainActivity.TAG, "thread end.");
Log.d(MainActivity.TAG, "Size: " + lastList.size()); // Size: 0
}
I expect the output of Size: 1, but the actual output is Size: 0.
java android arraylist
java android arraylist
asked Jan 4 at 0:33
a101a78a101a78
31
31
Are you sure that thread started withhandler.post()
already finished when you callsize()
on your list?
– Ivan
Jan 4 at 0:41
Yes, I called size () after confirming that the thread was completed through the thread.join () function.
– a101a78
Jan 4 at 0:54
No, you are joining with one thread, put you are passing anotherRunnable
tohandler.post()
which I think will be executed in one more thread.
– Ivan
Jan 4 at 2:36
add a comment |
Are you sure that thread started withhandler.post()
already finished when you callsize()
on your list?
– Ivan
Jan 4 at 0:41
Yes, I called size () after confirming that the thread was completed through the thread.join () function.
– a101a78
Jan 4 at 0:54
No, you are joining with one thread, put you are passing anotherRunnable
tohandler.post()
which I think will be executed in one more thread.
– Ivan
Jan 4 at 2:36
Are you sure that thread started with
handler.post()
already finished when you call size()
on your list?– Ivan
Jan 4 at 0:41
Are you sure that thread started with
handler.post()
already finished when you call size()
on your list?– Ivan
Jan 4 at 0:41
Yes, I called size () after confirming that the thread was completed through the thread.join () function.
– a101a78
Jan 4 at 0:54
Yes, I called size () after confirming that the thread was completed through the thread.join () function.
– a101a78
Jan 4 at 0:54
No, you are joining with one thread, put you are passing another
Runnable
to handler.post()
which I think will be executed in one more thread.– Ivan
Jan 4 at 2:36
No, you are joining with one thread, put you are passing another
Runnable
to handler.post()
which I think will be executed in one more thread.– Ivan
Jan 4 at 2:36
add a comment |
2 Answers
2
active
oldest
votes
Not sure what is handler
in your code, but seems like it makes http POST and then callback is executed in another thread after POST response. So, as I understand, you are making join on the thread thread
, but you are updating the list in another thread which is executed in POST callback. Since http call normally takes much longer then executing several lines of code, you don't see your list updated. To be sure - try to put Tread.sleep(10000)
after thread.join()
; and check if the size is 1. Also, putting some debug log statements would be really helpful here, put some log line after this lastList.add(output)
, so you can see the sequence of execution.
Then you can, probably, fix it by making join on your callback thread as well.
Just to demonstrate the idea I came up with this test code:
static Thread innerThread;
static ArrayList lastList = new ArrayList<>();
public static void main(String args) {
System.out.println("Thread main: started.");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread thread: started.");
innerThread = new Thread(new Runnable() {
public void run() {
System.out.println("Thread innerThread: started.");
try {
System.out.println("Thread innerThread: sleeping for 1 second.");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
lastList.add("test");
System.out.println("Thread innerThread: value added to the lastList");
System.out.println("Thread innerThread: ended.");
}
});
innerThread.start();
System.out.println("Thread thread: ended.");
}
});
thread.start();
try {
thread.join(); // to be sure that thread is finished - innerTread is initialized
innerThread.join(); // to be sure that innerTread is finished - lastList is updated
System.out.println("Thread main: lastList.size() = " + lastList.size()); // Size: 1
} catch (Exception e){
e.printStackTrace();
}
System.out.println("Thread main: ended.");
}
Output:
Thread main: started.
Thread thread: started.
Thread thread: ended.
Thread innerThread: started.
Thread innerThread: sleeping for 1 second.
Thread innerThread: value added to the lastList
Thread innerThread: ended.
Thread main: lastList.size() = 1
Thread main: ended.
Hope this will help!
Thanks for your help!
– a101a78
Jan 4 at 12:20
add a comment |
The problem is that:
handler.post(new Runnable() {
makes your:
lastList.add(output);
execute after your logging:
Log.d(MainActivity.TAG, "Size: " + lastList.size()); // Size: 0
From your code its not clear why you need to use handler.post
, its mostly used when you need to update UI widgets from worker thread.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54031711%2fthe-arraylist-add-method-does-not-work-within-a-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not sure what is handler
in your code, but seems like it makes http POST and then callback is executed in another thread after POST response. So, as I understand, you are making join on the thread thread
, but you are updating the list in another thread which is executed in POST callback. Since http call normally takes much longer then executing several lines of code, you don't see your list updated. To be sure - try to put Tread.sleep(10000)
after thread.join()
; and check if the size is 1. Also, putting some debug log statements would be really helpful here, put some log line after this lastList.add(output)
, so you can see the sequence of execution.
Then you can, probably, fix it by making join on your callback thread as well.
Just to demonstrate the idea I came up with this test code:
static Thread innerThread;
static ArrayList lastList = new ArrayList<>();
public static void main(String args) {
System.out.println("Thread main: started.");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread thread: started.");
innerThread = new Thread(new Runnable() {
public void run() {
System.out.println("Thread innerThread: started.");
try {
System.out.println("Thread innerThread: sleeping for 1 second.");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
lastList.add("test");
System.out.println("Thread innerThread: value added to the lastList");
System.out.println("Thread innerThread: ended.");
}
});
innerThread.start();
System.out.println("Thread thread: ended.");
}
});
thread.start();
try {
thread.join(); // to be sure that thread is finished - innerTread is initialized
innerThread.join(); // to be sure that innerTread is finished - lastList is updated
System.out.println("Thread main: lastList.size() = " + lastList.size()); // Size: 1
} catch (Exception e){
e.printStackTrace();
}
System.out.println("Thread main: ended.");
}
Output:
Thread main: started.
Thread thread: started.
Thread thread: ended.
Thread innerThread: started.
Thread innerThread: sleeping for 1 second.
Thread innerThread: value added to the lastList
Thread innerThread: ended.
Thread main: lastList.size() = 1
Thread main: ended.
Hope this will help!
Thanks for your help!
– a101a78
Jan 4 at 12:20
add a comment |
Not sure what is handler
in your code, but seems like it makes http POST and then callback is executed in another thread after POST response. So, as I understand, you are making join on the thread thread
, but you are updating the list in another thread which is executed in POST callback. Since http call normally takes much longer then executing several lines of code, you don't see your list updated. To be sure - try to put Tread.sleep(10000)
after thread.join()
; and check if the size is 1. Also, putting some debug log statements would be really helpful here, put some log line after this lastList.add(output)
, so you can see the sequence of execution.
Then you can, probably, fix it by making join on your callback thread as well.
Just to demonstrate the idea I came up with this test code:
static Thread innerThread;
static ArrayList lastList = new ArrayList<>();
public static void main(String args) {
System.out.println("Thread main: started.");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread thread: started.");
innerThread = new Thread(new Runnable() {
public void run() {
System.out.println("Thread innerThread: started.");
try {
System.out.println("Thread innerThread: sleeping for 1 second.");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
lastList.add("test");
System.out.println("Thread innerThread: value added to the lastList");
System.out.println("Thread innerThread: ended.");
}
});
innerThread.start();
System.out.println("Thread thread: ended.");
}
});
thread.start();
try {
thread.join(); // to be sure that thread is finished - innerTread is initialized
innerThread.join(); // to be sure that innerTread is finished - lastList is updated
System.out.println("Thread main: lastList.size() = " + lastList.size()); // Size: 1
} catch (Exception e){
e.printStackTrace();
}
System.out.println("Thread main: ended.");
}
Output:
Thread main: started.
Thread thread: started.
Thread thread: ended.
Thread innerThread: started.
Thread innerThread: sleeping for 1 second.
Thread innerThread: value added to the lastList
Thread innerThread: ended.
Thread main: lastList.size() = 1
Thread main: ended.
Hope this will help!
Thanks for your help!
– a101a78
Jan 4 at 12:20
add a comment |
Not sure what is handler
in your code, but seems like it makes http POST and then callback is executed in another thread after POST response. So, as I understand, you are making join on the thread thread
, but you are updating the list in another thread which is executed in POST callback. Since http call normally takes much longer then executing several lines of code, you don't see your list updated. To be sure - try to put Tread.sleep(10000)
after thread.join()
; and check if the size is 1. Also, putting some debug log statements would be really helpful here, put some log line after this lastList.add(output)
, so you can see the sequence of execution.
Then you can, probably, fix it by making join on your callback thread as well.
Just to demonstrate the idea I came up with this test code:
static Thread innerThread;
static ArrayList lastList = new ArrayList<>();
public static void main(String args) {
System.out.println("Thread main: started.");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread thread: started.");
innerThread = new Thread(new Runnable() {
public void run() {
System.out.println("Thread innerThread: started.");
try {
System.out.println("Thread innerThread: sleeping for 1 second.");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
lastList.add("test");
System.out.println("Thread innerThread: value added to the lastList");
System.out.println("Thread innerThread: ended.");
}
});
innerThread.start();
System.out.println("Thread thread: ended.");
}
});
thread.start();
try {
thread.join(); // to be sure that thread is finished - innerTread is initialized
innerThread.join(); // to be sure that innerTread is finished - lastList is updated
System.out.println("Thread main: lastList.size() = " + lastList.size()); // Size: 1
} catch (Exception e){
e.printStackTrace();
}
System.out.println("Thread main: ended.");
}
Output:
Thread main: started.
Thread thread: started.
Thread thread: ended.
Thread innerThread: started.
Thread innerThread: sleeping for 1 second.
Thread innerThread: value added to the lastList
Thread innerThread: ended.
Thread main: lastList.size() = 1
Thread main: ended.
Hope this will help!
Not sure what is handler
in your code, but seems like it makes http POST and then callback is executed in another thread after POST response. So, as I understand, you are making join on the thread thread
, but you are updating the list in another thread which is executed in POST callback. Since http call normally takes much longer then executing several lines of code, you don't see your list updated. To be sure - try to put Tread.sleep(10000)
after thread.join()
; and check if the size is 1. Also, putting some debug log statements would be really helpful here, put some log line after this lastList.add(output)
, so you can see the sequence of execution.
Then you can, probably, fix it by making join on your callback thread as well.
Just to demonstrate the idea I came up with this test code:
static Thread innerThread;
static ArrayList lastList = new ArrayList<>();
public static void main(String args) {
System.out.println("Thread main: started.");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread thread: started.");
innerThread = new Thread(new Runnable() {
public void run() {
System.out.println("Thread innerThread: started.");
try {
System.out.println("Thread innerThread: sleeping for 1 second.");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
lastList.add("test");
System.out.println("Thread innerThread: value added to the lastList");
System.out.println("Thread innerThread: ended.");
}
});
innerThread.start();
System.out.println("Thread thread: ended.");
}
});
thread.start();
try {
thread.join(); // to be sure that thread is finished - innerTread is initialized
innerThread.join(); // to be sure that innerTread is finished - lastList is updated
System.out.println("Thread main: lastList.size() = " + lastList.size()); // Size: 1
} catch (Exception e){
e.printStackTrace();
}
System.out.println("Thread main: ended.");
}
Output:
Thread main: started.
Thread thread: started.
Thread thread: ended.
Thread innerThread: started.
Thread innerThread: sleeping for 1 second.
Thread innerThread: value added to the lastList
Thread innerThread: ended.
Thread main: lastList.size() = 1
Thread main: ended.
Hope this will help!
edited Jan 4 at 2:03
answered Jan 4 at 0:46
Sergei SirikSergei Sirik
7771622
7771622
Thanks for your help!
– a101a78
Jan 4 at 12:20
add a comment |
Thanks for your help!
– a101a78
Jan 4 at 12:20
Thanks for your help!
– a101a78
Jan 4 at 12:20
Thanks for your help!
– a101a78
Jan 4 at 12:20
add a comment |
The problem is that:
handler.post(new Runnable() {
makes your:
lastList.add(output);
execute after your logging:
Log.d(MainActivity.TAG, "Size: " + lastList.size()); // Size: 0
From your code its not clear why you need to use handler.post
, its mostly used when you need to update UI widgets from worker thread.
add a comment |
The problem is that:
handler.post(new Runnable() {
makes your:
lastList.add(output);
execute after your logging:
Log.d(MainActivity.TAG, "Size: " + lastList.size()); // Size: 0
From your code its not clear why you need to use handler.post
, its mostly used when you need to update UI widgets from worker thread.
add a comment |
The problem is that:
handler.post(new Runnable() {
makes your:
lastList.add(output);
execute after your logging:
Log.d(MainActivity.TAG, "Size: " + lastList.size()); // Size: 0
From your code its not clear why you need to use handler.post
, its mostly used when you need to update UI widgets from worker thread.
The problem is that:
handler.post(new Runnable() {
makes your:
lastList.add(output);
execute after your logging:
Log.d(MainActivity.TAG, "Size: " + lastList.size()); // Size: 0
From your code its not clear why you need to use handler.post
, its mostly used when you need to update UI widgets from worker thread.
answered Jan 4 at 0:41
marcinjmarcinj
40.5k65378
40.5k65378
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54031711%2fthe-arraylist-add-method-does-not-work-within-a-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you sure that thread started with
handler.post()
already finished when you callsize()
on your list?– Ivan
Jan 4 at 0:41
Yes, I called size () after confirming that the thread was completed through the thread.join () function.
– a101a78
Jan 4 at 0:54
No, you are joining with one thread, put you are passing another
Runnable
tohandler.post()
which I think will be executed in one more thread.– Ivan
Jan 4 at 2:36