Java (Android) threads , accessing same list -


i'm new java , android bear me.

i have 1 arraylist of strings filling on main ui.

i have thread sending 1 one strings of arraylist through socket, , after sending each 1 erases list.

so it's fifo , 2 different threads accessing same arraylist.

how can make reading , writing on same list, thread safe? because read have to, preventing future errors.

my first thought creating synchronized method access arraylist.

this method created access arraylist both threads.

public synchronized string accessarraylist(boolean add, boolean get, string utt) {     if(add){                     mstrings.add(utt);         return null;     }     else if(get){         return mstrings.get(0);      }     else{         mstringsuttcomm.remove(0);         return null;     }   } 

the main thread add's strings list.

the second thread :

runnable runner = new runnable() {       public void run() {           while(!mstring.isempty()){                               //socket sends string                              sc.actionperformed(accessarraylist(false, true, null));                               //erase list               accessarraylist(false, false, null);           }        } }; 

is correct? because new eclipse , can't find way confirm 1 thread doesn't call accessarraylist if other 1 using it.

thank time.

take @ vectors , synchronization :

vectors : http://download.oracle.com/javase/1.4.2/docs/api/java/util/vector.html

synchronization : http://download.oracle.com/javase/tutorial/essential/concurrency/sync.html


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -