Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
linanmiao committed Jan 17, 2019
1 parent 6f7c9be commit 89b9966
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public AbstractCluster(String name) {

public AbstractCluster(String name, Class<T> interfaceClass, List<Consumer<T>> consumers, LoadBalance loadBalance) {
if (consumers == null || consumers.isEmpty()) {
throw new IllegalStateException("List<Consumer<T> consumers is null or empty.");
throw new IllegalStateException("List<Consumer<T>> consumers is null or empty.");
}
this.name = name;
this.interfaceClass = interfaceClass;
Expand Down
46 changes: 18 additions & 28 deletions cobia-core/src/main/java/lam/cobia/core/NotNegativeLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,33 @@ public NotNegativeLong(long initialValue) {
}

public long getAndIncrement() {
long oldValue = value.get();
long newValue = oldValue == Long.MAX_VALUE ? 0 : oldValue + 1;
while (!value.compareAndSet(oldValue, newValue)) {
oldValue = value.get();
newValue = oldValue == Long.MAX_VALUE ? 0 : oldValue + 1;
}
return oldValue;
long longs[] = compareAndAdd(Long.MAX_VALUE, 1);
return longs[0];
}

public long incrementAndGet() {
long oldValue = value.get();
long newValue = oldValue == Long.MAX_VALUE ? 0 : oldValue + 1;
while (!value.compareAndSet(oldValue, newValue)) {
oldValue = value.get();
newValue = oldValue == Long.MAX_VALUE ? 0 : oldValue + 1;
}
return newValue;
long longs[] = compareAndAdd(Long.MAX_VALUE, 1);
return longs[1];
}

public long getAndDecrement() {
long oldValue = value.get();
long newValue = oldValue == 0 ? 0 : oldValue - 1;
while (!value.compareAndSet(oldValue, newValue)) {
oldValue = value.get();
newValue = oldValue == 0 ? 0 : oldValue - 1;
}
return oldValue;
long longs[] = compareAndAdd(0, -1);
return longs[0];
}

public long decrementAndGet() {
long oldValue = value.get();
long newValue = oldValue == 0 ? 0 : oldValue - 1;
long longs[] = compareAndAdd(0, -1);
return longs[1];
}

private long[] compareAndAdd(long boundary, long addStep) {
long oldValue = get();
long newValue = oldValue == boundary ? boundary : oldValue + addStep;
while (!value.compareAndSet(oldValue, newValue)) {
oldValue = value.get();
newValue = oldValue == 0 ? 0 : oldValue - 1;
oldValue = get();
newValue = oldValue == boundary ? boundary : oldValue + addStep;
}
return newValue;
return new long[] {oldValue, newValue};
}

public long get() {
Expand All @@ -85,11 +75,11 @@ public static void main(String[] args) {
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
NotNegativeLong notNegativeLong = new NotNegativeLong();
NotNegativeLong notNegativeLong = new NotNegativeLong(10);

for (int i = 0; i < 100; i++) {
executorService.execute(() -> {
System.out.println(notNegativeLong.incrementAndGet());
System.out.println(notNegativeLong.getAndDecrement());
});
}
}
Expand Down
3 changes: 1 addition & 2 deletions cobia-core/src/main/java/lam/cobia/rpc/CountingProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public class CountingProvider<T> extends ProviderChainWrapper {
final NotNegativeLong invokedCount = new NotNegativeLong(0);

public CountingProvider(Provider<T> provider) {
super(provider, null);
start();
this(provider, null);
}

public CountingProvider(Provider<T> provider, ProviderChain next) {
Expand Down
23 changes: 8 additions & 15 deletions cobia-core/src/main/java/lam/cobia/rpc/DefaultProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,19 @@
*/
public class DefaultProtocol implements Protocol {

private final Object sharedObject;
private final Object sharedObject = new Object();

private ConcurrentMap<Consumer<?>, Object> consumerMap;
private final ConcurrentMap<Consumer<?>, Object> consumerMap = new ConcurrentHashMap<>();

private ConcurrentMap<String, Exporter<?>> exporterMap;
private final ConcurrentMap<String, Exporter<?>> exporterMap = new ConcurrentHashMap<>();

private ConcurrentMap<String, ExchangeServer> serverMap;
private final ConcurrentMap<String, ExchangeServer> serverMap = new ConcurrentHashMap<>();

private ConcurrentMap<Integer, lam.cobia.remoting.transport.netty.NettyServer> nettyMap;
private final ConcurrentMap<Integer, lam.cobia.remoting.transport.netty.NettyServer> nettyMap = new ConcurrentHashMap<>();

private ConcurrentMap<InetSocketAddress, lam.cobia.remoting.Client> clientsMap;
private final ConcurrentMap<InetSocketAddress, lam.cobia.remoting.Client> clientsMap = new ConcurrentHashMap<>();

public DefaultProtocol() {
this.sharedObject = new Object();
this.consumerMap = new ConcurrentHashMap<Consumer<?>, Object>();
this.exporterMap = new ConcurrentHashMap<String, Exporter<?>>();
this.serverMap = new ConcurrentHashMap<String, ExchangeServer>();
this.nettyMap = new ConcurrentHashMap<Integer, lam.cobia.remoting.transport.netty.NettyServer>();
this.clientsMap = new ConcurrentHashMap<InetSocketAddress, lam.cobia.remoting.Client>();
}


Expand All @@ -75,9 +69,8 @@ public <T> Exporter<T> export(Provider<T> provider, Map<String, Object> params)

openServer(provider);

String host = NetUtil.getLocalHost();
int port = ParameterUtil.getParameterInt(Constant.KEY_PORT, Constant.DEFAULT_SERVER_PORT);
HostAndPort hap = new HostAndPort().setHost(host).setPort(port);
final int port = ParameterUtil.getParameterInt(Constant.KEY_PORT, Constant.DEFAULT_SERVER_PORT);
HostAndPort hap = new HostAndPort().setHost(NetUtil.getLocalHost()).setPort(port);
//do work: registry provider
CRegistryBean.getRegistryProvider().registry(provider, hap, params);

Expand Down

0 comments on commit 89b9966

Please sign in to comment.