Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kalencaya committed Nov 29, 2024
2 parents b9a64a0 + 1afefca commit 0e41608
Show file tree
Hide file tree
Showing 51 changed files with 817 additions and 207 deletions.
11 changes: 5 additions & 6 deletions milky-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<artifactId>disruptor</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand All @@ -44,12 +49,6 @@
<artifactId>jackson-dataformat-properties</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.sliew</groupId>
<artifactId>milky-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cn.sliew.milky.common.concurrent;

public abstract class AbstractLoopRunnable implements LoopRunnable {

protected volatile boolean terminal = false;

@Override
public void terminate() {
terminal = true;
}

@Override
public boolean isTerminated() {
return terminal;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package cn.sliew.milky.thread;
package cn.sliew.milky.common.concurrent;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import static cn.sliew.milky.common.check.Ensures.checkNotNull;
import static cn.sliew.milky.common.check.Ensures.notBlank;

/**
* todo thread context。当创建线程的时候初始化线程的ThreadContext??
*/
class DaemonThreadFactory implements ThreadFactory {
public class DaemonThreadFactory implements ThreadFactory {

private final String threadNamePrefix;
private final int threadPriority;
Expand All @@ -23,15 +19,15 @@ public DaemonThreadFactory() {
this(null);
}

DaemonThreadFactory(String threadNamePrefix) {
public DaemonThreadFactory(String threadNamePrefix) {
this(threadNamePrefix, true);
}

public DaemonThreadFactory(String threadNamePrefix, int threadPriority) {
this(threadNamePrefix, threadPriority, true);
}

DaemonThreadFactory(String threadNamePrefix, boolean daemon) {
public DaemonThreadFactory(String threadNamePrefix, boolean daemon) {
this(threadNamePrefix, Thread.NORM_PRIORITY, daemon);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.sliew.milky.common.concurrent;

public interface LoopRunnable extends Runnable {

void execute();

void terminate();

boolean isTerminated();

@Override
default void run() {
while (isTerminated() == false) {
execute();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.sliew.milky.common.concurrent;

import java.util.concurrent.RejectedExecutionException;

public interface RunnableWrapper extends Runnable {

@Override
Expand All @@ -8,7 +10,9 @@ default void run() {
onBefore();
doRun();
onAfter();
} catch (Exception t) {
} catch (RejectedExecutionException t) {
onRejection(t);
} catch (Exception t) {
onFailure(t);
} finally {
onFinal();
Expand All @@ -27,6 +31,14 @@ default void run() {
*/
void onFailure(Exception e);

/**
* This should be executed if the thread-pool executing this action rejected the execution.
* The default implementation forwards to {@link #onFailure(Exception)}
*/
default void onRejection(Exception e) {
onFailure(e);
}

/**
* This method is called before all execution for init.
*/
Expand All @@ -48,4 +60,11 @@ default void onAfter() throws Exception {
default void onFinal() {
// nothing by default
}

/**
* Should the runnable force its execution in case it gets rejected?
*/
default boolean isForceExecution() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cn.sliew.milky.common.concurrent;

import java.util.concurrent.*;

public enum ThreadPoolUtil {
;

private static ConcurrentMap<String, ExecutorService> executors = new ConcurrentHashMap<>();

private static ExecutorService init(String poolName, int poolSize) {
return new ThreadPoolExecutor(poolSize, poolSize,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
new DaemonThreadFactory("Pool-" + poolName, true),
new ThreadPoolExecutor.CallerRunsPolicy());
}

public static ExecutorService getOrInitExecutors(String poolName, int poolSize) {
return executors.computeIfAbsent(poolName, key -> init(poolName, poolSize));
}

public static void releaseExecutors(String poolName) {
ExecutorService executorService = executors.remove(poolName);
if (executorService != null) {
executorService.shutdown();
}
}
}
132 changes: 132 additions & 0 deletions milky-common/src/main/java/cn/sliew/milky/common/unit/MoneyUnit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package cn.sliew.milky.common.unit;

import java.math.BigDecimal;

public enum MoneyUnit {

HAO {
@Override
public BigDecimal toHao(BigDecimal size) {
return size;
}

@Override
public BigDecimal toFen(BigDecimal size) {
return size.divide(C1.divide(C0));
}

@Override
public BigDecimal toJiao(BigDecimal size) {
return size.divide(C2.divide(C0));
}

@Override
public BigDecimal toYuan(BigDecimal size) {
return size.divide(C3.divide(C0));
}

@Override
public String getSuffix() {
return "毫";
}
},

FEN {

@Override
public BigDecimal toHao(BigDecimal size) {
return size.multiply(C1.divide(C0));
}

@Override
public BigDecimal toFen(BigDecimal size) {
return size;
}

@Override
public BigDecimal toJiao(BigDecimal size) {
return size.divide(C2.divide(C1));
}

@Override
public BigDecimal toYuan(BigDecimal size) {
return size.divide(C3.divide(C1));
}

@Override
public String getSuffix() {
return "分";
}
},

JIAO {
@Override
public BigDecimal toHao(BigDecimal size) {
return size.multiply(C2.divide(C0));
}

@Override
public BigDecimal toFen(BigDecimal size) {
return size.multiply(C2.divide(C1));
}

@Override
public BigDecimal toJiao(BigDecimal size) {
return size;
}

@Override
public BigDecimal toYuan(BigDecimal size) {
return size.divide(C3.divide(C2));
}

@Override
public String getSuffix() {
return "角";
}
},

YUAN {
@Override
public BigDecimal toHao(BigDecimal size) {
return size.multiply(C3.divide(C0));
}

@Override
public BigDecimal toFen(BigDecimal size) {
return size.multiply(C3.divide(C1));
}

@Override
public BigDecimal toJiao(BigDecimal size) {
return size.multiply(C3.divide(C2));
}

@Override
public BigDecimal toYuan(BigDecimal size) {
return size;
}

@Override
public String getSuffix() {
return "元";
}
};

static final BigDecimal _TEN = BigDecimal.TEN;
static final BigDecimal _HUNDRED = BigDecimal.valueOf(100L);
static final BigDecimal C0 = BigDecimal.ONE;
static final BigDecimal C1 = C0.multiply(_HUNDRED);
static final BigDecimal C2 = C1.multiply(_TEN);
static final BigDecimal C3 = C2.multiply(_TEN);

public abstract BigDecimal toHao(BigDecimal size);

public abstract BigDecimal toFen(BigDecimal size);

public abstract BigDecimal toJiao(BigDecimal size);

public abstract BigDecimal toYuan(BigDecimal size);

public abstract String getSuffix();
}
Loading

0 comments on commit 0e41608

Please sign in to comment.