关于线程问题,被彻底整晕了 直接上代码,网上看来的
private static int queueDeep = 4;
public void createThreadPool()
{
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 4, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueDeep), new ThreadPoolExecutor.DiscardOldestPolicy());
private synchronized int getQueueSize(Queue queue)
{
return queue.size();
}
public static void main(String[] args)
{
ThreadPoolExecutorTest test = new ThreadPoolExecutorTest();
test.createThreadPool();
}
我有一点不明白,方法getQueueSize为什么是synchronized ?这里只有主线程访问了createThreadPool()方法,不是多个线程在访问,何必要在一个方法上加synchronized 这个呢?