rbh920rbh
rbh920rbh
2015-03-27 12:51

Mongodb在java中,多线程update时报错,请问是哪里出现的问题?

  • java
  • thread
  • mongodb
 public class LocationPressureTest {
    private Todo todo;
    private int number;

    static Mongo mg = new Mongo("localhost", 27017);
    static DB db = mg.getDB("location_pressure_test_db");
    static DBCollection col = db.getCollection("location_pressure_test_col");


    public interface Todo {
        public void doIt();
    }
    public LocationPressureTest(int number, Todo todo) {
        this.number = number;
        this.todo = todo;
    }

    public void start() {
        for(int i = 0; i < number; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while(true) {
                        if(todo != null) {
                            try {
                                todo.doIt();
                            } catch (Throwable t) {
                                t.printStackTrace();
                                break;
                            }
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }).start();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        LocationPressureTest loadRunner = null;
        loadRunner = new LocationPressureTest(347, new Todo() {
            @Override
            public void doIt() {
                System.out.println(Thread.currentThread());
                col.update(col.findOne(new BasicDBObject("_id", String.valueOf(Math
                        .round(Math.random() * 5000000)))),
                        new BasicDBObject("$set", new BasicDBObject("coordinates",
                                new Double[] { Math.random() * 360 - 180,
                                        Math.random() * 360 - 180 })));
            }
        });
        loadRunner.start();

        synchronized (loadRunner) {
            try {
                loadRunner.wait();
            } catch (Exception e) {
            }
        }
    }

}

报错如下

 com.mongodb.MongoWaitQueueFullException: Too many threads are already waiting for a connection. Max number of threads (maxWaitQueueSize) of 50 has been exceeded.
    at com.mongodb.connection.DefaultConnectionPool.createWaitQueueFullException(DefaultConnectionPool.java:259)
    at com.mongodb.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:87)
    at com.mongodb.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:80)
    at com.mongodb.connection.DefaultServer.getConnection(DefaultServer.java:69)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.getConnection(ClusterBinding.java:86)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:182)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:175)
    at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:105)
    at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:57)
    at com.mongodb.Mongo.execute(Mongo.java:745)
    at com.mongodb.Mongo$2.execute(Mongo.java:728)
    at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:327)
    at com.mongodb.DBCollection.update(DBCollection.java:461)
    at com.mongodb.DBCollection.update(DBCollection.java:426)
    at com.mongodb.DBCollection.update(DBCollection.java:485)
    at com.mongodb.DBCollection.update(DBCollection.java:500)
    at load.LocationPressureTest$2.doIt(LocationPressureTest.java:68)
    at load.LocationPressureTest$1.run(LocationPressureTest.java:43)
    at java.lang.Thread.run(Unknown Source)
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答