Home  >  Q&A  >  body text

Java多线程操作集合的小问题

PHP中文网PHP中文网2763 days ago344

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:14:23

    ExecutorService exec = Executors.newFixedThreadPool(8);
    for(final Object obj : list){
        exec.execute(new Runnable() {
            @Override
            public void run() {
                process(obj)
            }
        });
    }
    
    

    I think this method is better.

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:14:23

    java8 parallel stream

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:14:23

    The member variable index is a multi-thread shared variable. You need to add volatile to ensure the visibility of this variable in multi-threads. The declaration code is changed to the following:

    private volatile int index = 0;

    Specifically why please learn the basic knowledge related to java concurrent programming

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:14:23

    It’s best to use thread synchronization in your run()

    reply
    0
  • Cancelreply