java - How to change a variable via a worker thread -


is there elegant way that? or can avoided because 1 can use better design patter?

import java.util.arraylist; import java.util.list;  public class fortest {      list<string> ls = new arraylist<string>();      public static void main(string[] args) {         fortest fortest=new fortest();         system.out.println(fortest.ls.size());          new thread(new worker(fortest.ls)).start();         //size() not change @         system.out.println(fortest.ls.size());     } }  class worker implements runnable{     list<string> list;     public worker(list<string> li) {         this.list = li;     }      public void run(){         this.list.add("newitem");     } } 

there several issues code (in particular use arraylist not thread safe without proper synchronization).

but obvious 1 second println statement going called before run method has had chance executed.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -