如何使用索引运行多个线程一个接一个共享阵列列表



我想要登录多个帐户,所以这就是为什么我使用线程..它的工作正常有时,但有时相同的帐户记录了多次而不是剩余帐户...任何人都会给出建议。<</p>

  gmail_synclist=new CopyOnWriteArrayList<>();
                gmail_synclist.clear();
                gmail_synclist = Helpers.getArrayList(WelcomeMessage.this, "email_creds");
            List<Thread> threads = new ArrayList<Thread>();
            for( int i = 0; i < gmail_creds.size(); i++){
                final int j = i;
                Thread t = new Thread(new Runnable() {
                    public void run(){
         synchronized (gmail_synclist) {
                        Helpers.sharedPreferencess(WelcomeMessage.this,"userEmail", gmail_creds.get(j).userEmail);
                        Helpers.sharedPreferencess(WelcomeMessage.this,"userPassword",gmail_creds.get(j).userPassword);
                        AccountSetupBasics.actionNewAccount(WelcomeMessage.this);
        }
                    }
                })
                t.start();
                threads.add(t);
            }
            // Let all threads to finish execution prior continuing main thread.
            try {
                for(Threat t: threads){
                    t.join();
                }
            } catch(InterruptedException ie){
                ie.printStackTrace();
            }

好的,但格式不正确,我将显示b/w frag and anderler thread

//一个简单的处理程序线程clas

public class MyHandlerThread extends HandlerThread {
private final static String TAG_THREAD = MyHandlerThread.class.getSimpleName();
public MyHandlerThread() {
    super("MyHandlerThread");
    Log.e(TAG_THREAD, " ctor ");
}    
  }

//一个虚拟片段

private MyHandlerThread myHandlerThread;
public Handler mWorkerHandler;
int MSG_ONE;
int MSG_TWO;

@Override
public void onViewCreated(whatever) {
 // start the thread
    threadOperation();
// send a message
    sendMessage(MSG_ONE)
}

private void threadOperation() {
    System.out.println("tread started");
    myHandlerThread = new MyHandlerThread();
    myHandlerThread.start();
    Looper looper = myHandlerThread.getLooper();        
    mWorkerHandler = new Handler(looper, new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

         if(msg.what == MSG_ONE){
           // do your work based on that
          // these are all in bankground thread, so any ui updation task
          // use runOnUiTHread() or main thread handler to post task
            }
            else if(msg.what == MSG_TWO){
            // do work based on two
            }
            return true;
        }
    });
}
public void sendMessage(int whatMsg) {
    System.out.println("send message --------------------");
    doSimulation();
    Message message = mWorkerHandler.obtainMessage(whatMsg);
    mWorkerHandler.sendMessage(message);
    Log.e(TAG, "message sent ..");
}

最新更新