目前我想通过往任务队列扔任务,观察内存变动,来确定任务的大小。但是目前没效果。
线程池代码:
public class CommonThreadPool { public final static int CORE_SIZE = 1; public final static int MAX_SIZE = 1; public final static long KEEP_ALIVE_TIME = 3L; public BlockingQueue blockingQueue = new ArrayBlockingQueue(100000); private ExecutorService executorService = new ThreadPoolExecutor( CORE_SIZE, MAX_SIZE, KEEP_ALIVE_TIME, TimeUnit.MINUTES, blockingQueue, new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread("threadname"); } }, new DiscardPolicy() ); public ExecutorService getExecutorService() { return executorService; } }
使用线程池的代码
public static void main(String[] args) throws InterruptedException { ExecutorService executorService = new CommonThreadPool().getExecutorService(); for (int i = 0; i < 10000; i++) { executorService.execute(new Runnable() { @Override public void run() { byte[] bytes = new byte[1024]; try { // 500ms,以便保证线程未处理完任务,新来的任务直接进入任务队列 Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }); } }
观察内存没有变化:
但是按理来说内存应该会有变化,但是现在没有变化,不知道为啥。
CORE_SIZE, MAX_SIZE就1个线程在干活
排队的空间ArrayBlockingQueue(100000),这些已经分配了,运行的过程中应该看不到太大的起伏也正常
排队的空间已经分配好了;但是每个任务里面也占1M空间。byte[] bytes = new byte[1024];
@不知怎么办才好:
采样的频率和你程序的频率如果基本一致,看不到起伏也正常
设置MAX_SIZE 3 5个, Thread.sleep(500) 时间再长点让内存交错出现,估计就能看到起伏