- 缓存
- 类型转化
- 字符分割
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
package com.steven.springboot.redis.utils;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import javax.validation.constraints.Max;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class GuavaUtil {
private static Cache<String, Object> dataMap = CacheBuilder.newBuilder()
.initialCapacity(100)
.maximumSize(1000)
.concurrencyLevel(5)
.expireAfterWrite(3, TimeUnit.SECONDS)
.build();
public static void put(String key, Object value) {
dataMap.put(key, value);
}
public static Object get(String key) {
return dataMap.getIfPresent(key);
}
public static void remove(String key) {
dataMap.invalidate(key);
}
public static String spliter(List<Object> list) {
return Joiner.on(",").join(list);
}
public static String mapToString(Map<String, Object> map) {
return Joiner.on(",").withKeyValueSeparator("=").join(map);
}
public static List<String> stringToList(String str){
return Splitter.on(",").splitToList(str);
}
public static void main(String[] args) throws InterruptedException {
List<String> strings = stringToList("1,2,3,4");
System.out.println(strings);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
public static void main(String[] args) throws InterruptedException {
ThreadFactory factory = new DefaultThreadFactory("task", 4);
ExecutorService threadPool = new ThreadPoolExecutor(3, 6, 20, TimeUnit.MINUTES, new ArrayBlockingQueue<>(30), factory, new ThreadPoolExecutor.DiscardPolicy());
ListeningExecutorService service = MoreExecutors.listeningDecorator(threadPool);
ListenableFuture<String> future = service.submit(() -> {
System.out.println("任务在执行!!!");
return "task";
});
future.addListener(()->{
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}, service);
Futures.addCallback(future, new FutureCallback<String>(){
@Override
public void onSuccess(String s) {
System.out.println("callback result:"+s);
}
@Override
public void onFailure(Throwable throwable) {
System.out.println(throwable.getMessage());
}
},service);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30