Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
黄志磊 committed Jan 27, 2016
1 parent 964a1e4 commit 709136a
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 61 deletions.
2 changes: 1 addition & 1 deletion mpush-tools/pom.xml
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>mpush-tools</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
Expand Down
Expand Up @@ -9,47 +9,51 @@
public class ThreadPoolContext {

private final String name;//名字
private final int cores; //最小线程大小
private final int threads; //最大线程大小
private final int queues; // queues > 0,则FIFO队列,
private final int alive;// 存活时间
private final int corePoolSize; //最小线程大小
private final int maxPoolSize; //最大线程大小
private final int queueCapacity; // 允许缓冲在队列中的任务数 (0:不缓冲、负数:无限大、正数:缓冲的任务数)
private final int keepAliveSeconds;// 存活时间

public static ThreadPoolContext BOSS_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POLL_SIZE, 1000*60*5);
public static ThreadPoolContext BOSS_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_BOSS, Constants.MIN_BOSS_POOL_SIZE, Constants.MAX_BOSS_POLL_SIZE, 60*5);

public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 1000*60*5);
public static ThreadPoolContext WORK_THREAD_POOL = CachedThreadPoolContext.create(ThreadNameSpace.NETTY_WORKER, Constants.MIN_WORK_POOL_SIZE, Constants.MAX_WORK_POOL_SIZE, 60*5);

public static ThreadPoolContext BIZ_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.BIZ, Constants.BIZ_POOL_SIZE);

public static ThreadPoolContext EVENT_BUS_THREAD_POOL = FixedThreadPoolContext.create(ThreadNameSpace.EVENT_BUS, Constants.EVENT_BUS_POOL_SIZE);

public ThreadPoolContext(String name, int cores, int threads, int queues, int alive) {
public ThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds) {
this.name = name;
this.cores = cores;
this.threads = threads;
this.queues = queues;
this.alive = alive;
this.corePoolSize = corePoolSize;
this.maxPoolSize = maxPoolSize;
this.queueCapacity = queueCapacity;
this.keepAliveSeconds = keepAliveSeconds;
}

public String getName() {
return name;
}
public int getCores() {
return cores;

public int getCorePoolSize() {
return corePoolSize;
}
public int getThreads() {
return threads;

public int getMaxPoolSize() {
return maxPoolSize;
}
public int getQueues() {
return queues;

public int getQueueCapacity() {
return queueCapacity;
}
public int getAlive() {
return alive;

public int getKeepAliveSeconds() {
return keepAliveSeconds;
}

@Override
public String toString() {
return "ThreadPoolContext [name=" + name + ", cores=" + cores + ", threads=" + threads + ", queues=" + queues + ", alive=" + alive + "]";
return "ThreadPoolContext [name=" + name + ", corePoolSize=" + corePoolSize + ", maxPoolSize=" + maxPoolSize + ", queueCapacity=" + queueCapacity + ", keepAliveSeconds=" + keepAliveSeconds
+ "]";
}


}
@@ -1,10 +1,8 @@
package com.shinemo.mpush.tools.thread.threadpool;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;


import com.shinemo.mpush.tools.spi.ServiceContainer;
Expand Down Expand Up @@ -33,19 +31,6 @@ public static final Map<String, Executor> getPool(){
return poolCache;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("current thread allocation policy:");
Iterator<Map.Entry<String, Executor>> ite = poolCache.entrySet().iterator();
while (ite.hasNext()) {
Map.Entry<String, Executor> entry = ite.next();
String serviceUniqName = entry.getKey();
ThreadPoolExecutor executor = (ThreadPoolExecutor)entry.getValue();
sb.append("serviceName[" + serviceUniqName + "]coreThreadNums:" + executor.getCorePoolSize() + " maxThreadNums:" + executor.getMaximumPoolSize() + " activityThreadNums:"
+ executor.getActiveCount());
}

return sb.toString();
}


}
Expand Up @@ -23,23 +23,23 @@ public class CachedThreadPool implements ThreadPool {
public Executor getExecutor(ThreadPoolContext context) {

String name = context.getName();
int cores = context.getCores();
int threads = context.getThreads();
int queues = context.getQueues();
int alive = context.getAlive();
int corePoolSize = context.getCorePoolSize();
int maxPoolSize = context.getMaxPoolSize();
int queueCapacity = context.getQueueCapacity();
int keepAliveSeconds = context.getKeepAliveSeconds();

final ThreadFactory threadFactory = new NamedThreadFactory(name);

BlockingQueue<Runnable> blockingQueue = null;
if(queues == 0){
if(queueCapacity == 0){
blockingQueue = new SynchronousQueue<Runnable>();
}else if(queues<0){
}else if(queueCapacity<0){
blockingQueue = new LinkedBlockingQueue<Runnable>();
}else{
blockingQueue = new LinkedBlockingQueue<Runnable>(queues);
blockingQueue = new LinkedBlockingQueue<Runnable>(queueCapacity);
}

return new ThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context));
return new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, TimeUnit.SECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context));

}

Expand Down
Expand Up @@ -4,12 +4,12 @@

public class CachedThreadPoolContext extends ThreadPoolContext{

public CachedThreadPoolContext(String name, int cores, int threads, int alive) {
super(name, cores, threads, 0, alive);
public CachedThreadPoolContext(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds) {
super(name, corePoolSize, maxPoolSize, 0, keepAliveSeconds);
}

public static CachedThreadPoolContext create(String name, int cores, int threads, int alive){
return new CachedThreadPoolContext(name, cores, threads, alive);
public static CachedThreadPoolContext create(String name, int corePoolSize, int maxPoolSize, int keepAliveSeconds){
return new CachedThreadPoolContext(name, corePoolSize, maxPoolSize, keepAliveSeconds);
}

}
Expand Up @@ -22,21 +22,21 @@ public class FixedThreadPool implements ThreadPool {
@Override
public Executor getExecutor(ThreadPoolContext context) {
String name = context.getName();
int threads = context.getThreads();
int queues = context.getQueues();
int corePoolSize = context.getCorePoolSize();
int queueCapacity = context.getQueueCapacity();

BlockingQueue<Runnable> blockingQueue = null;
if (queues == 0) {
if (queueCapacity == 0) {
blockingQueue = new SynchronousQueue<Runnable>();
} else if (queues < 0) {
} else if (queueCapacity < 0) {
blockingQueue = new LinkedBlockingQueue<Runnable>();
} else {
blockingQueue = new LinkedBlockingQueue<Runnable>(queues);
blockingQueue = new LinkedBlockingQueue<Runnable>(queueCapacity);
}

final ThreadFactory threadFactory = new NamedThreadFactory(name);

return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context));
return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.SECONDS, blockingQueue, threadFactory, new IgnoreRunsPolicy(context));
}

}
Expand Up @@ -4,12 +4,20 @@

public class FixedThreadPoolContext extends ThreadPoolContext{

public FixedThreadPoolContext(String name, int threads,int queueCapacity) {
super(name, threads, 0, queueCapacity, 0);
}

public FixedThreadPoolContext(String name, int threads) {
super(name, 0, threads, -1, 0);
super(name, threads, 0, -1, 0);
}

public static FixedThreadPoolContext create(String name,int threads){
return new FixedThreadPoolContext(name, threads);
}

public static FixedThreadPoolContext create(String name,int threads,int queueCapacity){
return new FixedThreadPoolContext(name, threads,queueCapacity);
}

}
@@ -0,0 +1,16 @@
package com.shinemo.mpush.tools.thread;


import org.junit.Test;


public class SyncTest {



@Test
public void test(){

}

}
6 changes: 3 additions & 3 deletions pom.xml
Expand Up @@ -31,7 +31,7 @@
<java.version>1.7</java.version>
<spring-version>4.0.0.RELEASE</spring-version>
<mpush-api-version>1.0-SNAPSHOT</mpush-api-version>
<mpush-tools-version>1.0-SNAPSHOT</mpush-tools-version>
<mpush-tools-version>1.0.1-SNAPSHOT</mpush-tools-version>
<mpush-common-version>1.0-SNAPSHOT</mpush-common-version>
<mpush-netty-version>1.0-SNAPSHOT</mpush-netty-version>
<mpush-core-version>1.0-SNAPSHOT</mpush-core-version>
Expand Down Expand Up @@ -231,7 +231,7 @@
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- <plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
Expand All @@ -248,7 +248,7 @@
</goals>
</execution>
</executions>
</plugin> -->
</plugin>
</plugins>
</build>

Expand Down

0 comments on commit 709136a

Please sign in to comment.