Java 多线程数量

在Java并发编程实践一书中有提到,同时并发线程数量等于CPU核心数-1. 这个道理很浅显,就是要确保多个线程可以同时并发从而效率达到最高。今天写了个程序,发现故事的真相不是这样的....


先写一个具有共享资源的类

package com.lenovo.plm.dms.p5;

public class Service {
    
    
    private long record;
    
    public Service(long record){
        this.record = record;
    }
    
    
    synchronized public void addRecord(){
        
        System.out.println(Thread.currentThread().getName() + ":"+ System.currentTimeMillis() +"------------->" + record++);
        
    }
    
}

然后线程类

package com.lenovo.plm.dms.p5;

public class MyThread extends Thread {
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();
        Service service = new Service(0);
        for(int i = 0;i<1000;i++){
            service.addRecord();
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
}

最后执行类:

package com.lenovo.plm.dms.p5;

public class Main {
    
    public static void main(String[] args) {
        
        
        for(int i = 0;i<40;i++){
            MyThread t = new MyThread();
            t.start();
        }
        
    }
    
}


这里一共创建了40个线程,看执行结果,比较长:

Thread-0:1455797512547------------->0
Thread-6:1455797512547------------->0
Thread-7:1455797512547------------->0
Thread-9:1455797512547------------->0
Thread-8:1455797512547------------->0
Thread-13:1455797512547------------->0
Thread-4:1455797512547------------->0
Thread-1:1455797512547------------->0
Thread-2:1455797512547------------->0
Thread-3:1455797512547------------->0
Thread-15:1455797512547------------->0
Thread-12:1455797512547------------->0
Thread-11:1455797512547------------->0
Thread-5:1455797512547------------->0
Thread-14:1455797512547------------->0
Thread-16:1455797512547------------->0
Thread-17:1455797512547------------->0
Thread-10:1455797512547------------->0
Thread-18:1455797512547------------->0
Thread-19:1455797512547------------->0
Thread-20:1455797512547------------->0
Thread-21:1455797512547------------->0
Thread-22:1455797512547------------->0
Thread-23:1455797512547------------->0
Thread-24:1455797512547------------->0
Thread-25:1455797512547------------->0
Thread-26:1455797512547------------->0
Thread-27:1455797512547------------->0
Thread-28:1455797512547------------->0
Thread-29:1455797512547------------->0
Thread-30:1455797512547------------->0
Thread-31:1455797512547------------->0
Thread-32:1455797512547------------->0
Thread-33:1455797512547------------->0
Thread-34:1455797512547------------->0
Thread-35:1455797512547------------->0
Thread-36:1455797512547------------->0
Thread-37:1455797512547------------->0
Thread-38:1455797512562------------->0
Thread-39:1455797512562------------->0


结果是,并发执行的线程数量达到了38个。因为这里一共启动了40个线程,其中38个线程是同一时间(1455797512547)执行的。因此可以确定,并发线程数量和CPU核心数的关系不是一比一的关系了。

另外,JVM到底可以支持多少个线程,google了一下,有了这样一个文章:http://jzhihui.iteye.com/blog/1271122 这里做个记录。
















你可能感兴趣的:(java,多线程)