程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> mongodb-Mongodb在java中,多線程update時報錯,請問是哪裡出現的問題?

mongodb-Mongodb在java中,多線程update時報錯,請問是哪裡出現的問題?

編輯:編程綜合問答
Mongodb在java中,多線程update時報錯,請問是哪裡出現的問題?
 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)

最佳回答:


不用設置mongodb的線程數,只需要把number變量的值設小一點就行了,比如10.

bulusli3
rbh920rbh
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved