了解 Netty 3 中 MemoryAwareThreadPoolExecutor 的行为
understanding the behavior of MemoryAwareThreadPoolExecutor in Netty 3
我想了解 MemoryAwareThreadPoolExecutor 在 Netty 3 中的行为。
我正在实施 java docs 中提供的示例,只是稍作改动。
我的可运行class
class MyRunnable implements Runnable {
private final byte[] data;
public byte[] getData() {
return data;
}
public MyRunnable(byte[] data) {
this.data = data;
}
public void run() {
String dataString = new String(data, 0, data.length);
System.out.println("Started processing data " + dataString);
try {
TimeUnit.MILLISECONDS.sleep(3000);
//Thread.sleep(3000);
} catch (InterruptedException ie){
ie.printStackTrace();
}
System.out.println("Done processing data " + dataString);
}
}
MyObjectSizeEstimator class 是
class MyObjectSizeEstimator extends DefaultObjectSizeEstimator {
@Override
public int estimateSize(Object o) {
if (o instanceof MyRunnable) {
return ((MyRunnable) o).getData().length;
}
return super.estimateSize(o);
}
}
主要 class 是
public class MemoryAwareThreadPoolExecutorDemo {
public static void main(String[] args) {
ThreadPoolExecutor pool = new MemoryAwareThreadPoolExecutor(
16, 65536, 1048576, 30, TimeUnit.SECONDS,
new MyObjectSizeEstimator(),
Executors.defaultThreadFactory());
String[] dataArray = new String[10];
for(int i = 0; i < dataArray.length; ++i) {
dataArray[i] = RandomStringUtils.randomAlphanumeric((i + 1) ) + " " + i;
}
for(int i = 0; i < dataArray.length; ++i) {
pool.execute(new MyRunnable(dataArray[i].getBytes()));
}
while(pool.getActiveCount() != 0) {
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
pool.shutdown();
}
}
我期待主要 class 执行更大的任务。但是我看到每次我 运行 程序时,任务总是以随机顺序执行。
程序行为背后的任何解释或原因。
MemoryAwareThreadPoolExecutor
基本上只知道任务将占用多少内存,并根据它处理 "back-pressure"。跟下单一点关系都没有。
另请注意,Netty 3 已停产很长时间,您应该使用 4.1。
我想了解 MemoryAwareThreadPoolExecutor 在 Netty 3 中的行为。
我正在实施 java docs 中提供的示例,只是稍作改动。
我的可运行class
class MyRunnable implements Runnable {
private final byte[] data;
public byte[] getData() {
return data;
}
public MyRunnable(byte[] data) {
this.data = data;
}
public void run() {
String dataString = new String(data, 0, data.length);
System.out.println("Started processing data " + dataString);
try {
TimeUnit.MILLISECONDS.sleep(3000);
//Thread.sleep(3000);
} catch (InterruptedException ie){
ie.printStackTrace();
}
System.out.println("Done processing data " + dataString);
}
}
MyObjectSizeEstimator class 是
class MyObjectSizeEstimator extends DefaultObjectSizeEstimator {
@Override
public int estimateSize(Object o) {
if (o instanceof MyRunnable) {
return ((MyRunnable) o).getData().length;
}
return super.estimateSize(o);
}
}
主要 class 是
public class MemoryAwareThreadPoolExecutorDemo {
public static void main(String[] args) {
ThreadPoolExecutor pool = new MemoryAwareThreadPoolExecutor(
16, 65536, 1048576, 30, TimeUnit.SECONDS,
new MyObjectSizeEstimator(),
Executors.defaultThreadFactory());
String[] dataArray = new String[10];
for(int i = 0; i < dataArray.length; ++i) {
dataArray[i] = RandomStringUtils.randomAlphanumeric((i + 1) ) + " " + i;
}
for(int i = 0; i < dataArray.length; ++i) {
pool.execute(new MyRunnable(dataArray[i].getBytes()));
}
while(pool.getActiveCount() != 0) {
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
pool.shutdown();
}
}
我期待主要 class 执行更大的任务。但是我看到每次我 运行 程序时,任务总是以随机顺序执行。
程序行为背后的任何解释或原因。
MemoryAwareThreadPoolExecutor
基本上只知道任务将占用多少内存,并根据它处理 "back-pressure"。跟下单一点关系都没有。
另请注意,Netty 3 已停产很长时间,您应该使用 4.1。