摘要:任务被拒绝添加后,会调用当前线程池的线程来执行被拒绝的任务。被线程池拒绝的任务会被直接丢弃,不会抛出或执行任何异常。
在开发equals()方法时使用null.equals()将会报告一个空指针,因为它是非空值。equals()可以使用对象的equals()方法避免空值,完美stringstone = nullString strTwo = nullboolean one flag = objects . equals(strOne,strTwo);忽略case: equalsIgnoreCase()创建HashMap指定初始化大小public HashMap(int initial capacity){ this(initial capacity,default _ load _ factor);} initialCapacity =(要存储的元素数量/加载因子)+1。默认负载系数为0.75。
当HashMap的大小没有指定时,会发生多次扩展,影响效率。例如,如果地图中有1000个元素,则容量至少应设置为1000/0.75=133宝塔1=1334。如果不设置大小,容量会扩大很多倍,比较会影响效率。
当无法估计大小时,请将其设置为HashMap的默认大小16。
Optional.ofNullable()。orElse()避免空指针通过Optional.ofNullable()避免空指针。orElse(),比如遍历从map中获取的某个列表。原始代码如下:
map map = new HashMap(16);map.put("list ",null);List List =(List)map . get(" List ");// list将报告(mapitem:list){ logger . info(string . value of(item))的控制指针;}使用Optional完美求解Map Map = new HashMap(16);map.put("list ",null);List List = optional . of nullable((List)map . get(" List "))。or else(new ArrayList());for(Map item:list){ logger . info(string . value of(item));}使用可选选择默认数据用户user1 =新用户(1,“张三”,“Xi安”);User2 =新用户(2,“李四”,“新疆”);user1 = nulluser user = optional . of nullable(user 1)。orElseGet(()-> user 2);logger . info(user . tostring());流求和用Stream flow遍历求和运算列表double list = arrays.aslist (12.2,13.45,12.4)集合中的元素;double sumOne = doubleList.stream()。mapto double(x ^ -> x)。sum();logger.info("sumOne:{} ",sumOne);list decimal list = new ArrayList();decimal list . add(new BigDecimal(" 12.3 "));decimal list . add(new BigDecimal(" 12.3 "));decimal list . add(new BigDecimal(" 12.3 "));BigDecimal BigDecimal = decimal list . stream()。reduce(BigDecimal。ZERO,BigDecimal::add);logger.info("bigDecimal:{} ",bigDecimal . float value());logger.info("bigDecimal:{} ",bigDecimal . double value());list object list = arrays . as list(12,13,1);int sum = objectList.stream()。mapToInt(x ^ ->(int)x)。sum();logger.info("sum:{} ",sum);列表切割工具lists . partition()for(List datadtos:lists . partition(List,1000)){ datadtomapper . insertbatch(datadtos);}依靠com . Google . guava guava 31 . 0 . 1-JRE log4j log4j singleton类或工具类添加私有构造方法。工具类或singleton类提供了一个没有太多函数的私有构造函数,提供了一个私有构造函数防止外部使用污染。类(集合)应该在使用后清空。为了避免内存泄漏,无用的对象应该及时销毁。如果对象太大,虚拟机无法垃圾收集,可能会造成内存泄漏,所以清空。使用后私有静态最终映射table _ column _ map = new hashmap();//清空集合TABLE _ COLUMN _ map . clear();使用ThreaadLocal后,需要释放资源以防止内存泄漏。私有线程本地线程本地映射=新线程本地();@ test public void test1(){ Map Map = new HashMap();Map.put("test1 ","张三");Map.put("test2 ","李四");threadlocalmap . set(map);getThreadLocalMap();threadlocalmap . remove();}巧妙运用设计模式,巧妙运用接口,抽象类
请看:Java设计模式的https://blog.csdn.net/qq_37248504/article/details/117753021。
Spring Factory模式和Java Factory模式的结合请看:https://blog.csdn.net/qq_37248504/article/details/117753021使用Spring收集Bean对象,并把它们放到一个Map中,从中获取对应的对象实例。去直接代码接口CustomerService.java公共接口customerservice {/* * *获取用户名* @ return */String get username();/* * * register */void registed();/* * * log in */void log in();}接口的实现类:要收集的对象可以有多个实例@ service公共类customerserviceoneimplements customerservice { @ override public string get username(){ return " customersene ";} @ Override public void registered(){ } @ Override public void log in(){ } } @ service public类CustomerServiceTwoImpl实现CustomerService { @ Override public String get username(){ return " customer two ";} @ override public void registered(){ } @ override public void log in(){ } } Factory方法:可以使用set injection或constructor,也可以从当前上下文中获取接口的instance object @ component公共类customer Factory {/* * * object map */private static final map object _ map = new hashmap();/** * set方法注入* * @ param CustomerService */@ autowired private void set OBJECT map(listcustomerservice){ for(CustomerService service:CustomerService){ OBJECT _ map . put(service . get username(),service);}}/* * *获取CustomerService的实例* * @ param type * @ return */public static CustomerService GetInstance(string type){ return object _ map . Get(type);}} Test /** * Spring factory模式Test */@ override public void Test five(){ customer factory . getinstance(" customer one ")。已注册();customer factory . getinstance(" customer two ")。已注册();}请看:https://gitee.com/Marlon _ Brando/back/commit/d 9 ad 24564 dfe 1459 EC 9 e 0728822 b 709 E6 fa 41 spring,SpringBoot经常使用技能来获取容器中的bean以及bean的初始化。
更多详情,请见:https://blog.csdn.net/qq_37248504/article/details/113896952
流等。使用streamlistcollect = list.stream()方法的引用。Filter ("Lisi":: equals)。Map (string:: toupper case)。collect(collectors . to list());collect . foreach(system . out::println);List.removeIf("李四"::equals);HashMap Map = new HashMap(){ { put(" one "," one ");放(“二”、“一”);放(“三”、“一”);}};map mapOne = new HashMap();map . foreach(mapOne::put);logger . info(string . value of(mapOne));在Java中创建线程池使用ThreadPoolExecutor创建线程池,使用线程。new Thread()不是到处回收,造成资源浪费,因为要交给线程池管理线程。公共类ThreadPooTest { private static final Logger Logger = Logger factory . get Logger(ThreadPooTest . class);private static final long THREAD _ TIME _ OUT = 60;@ Test public void Test(){//Cpu cores intcpunum = runtime . get runtime()。available processors();//最大数量int maxSize = 2 * cpuNum+1;/* * *拒绝策略:当阻塞队列和最大线程用完* * AbortPolicy:线程池执行器中默认的拒绝策略是AbortPolicy。直接抛出异常。* CallerRunsPolicy:任务被拒绝添加后,会调用当前线程池的线程来执行被拒绝的任务。* DiscardPolicy:被线程池拒绝的任务会被直接丢弃,不会抛出或执行任何异常。* DiscardOldestPolicy:当一个任务拒绝添加时,它会丢弃任务队列中最老的任务,也就是第一个加入队列的任务,然后添加这个新任务。*/ThreadPoolExecutor pool executor = new ThreadPoolExecutor(CPU num,maxSize,THREAD_TIME_OUT,TimeUnit。秒,new ArrayBlockingQueue(20),Executors.defaultThreadFactory(),new ThreadPoolExecutor。abort policy());poolExecutor.execute(新线程(()-> { logger . info(Thread . current Thread()。toString());}));}} Spring Boot配置全局线程池@ configuration @ enableasync public类thread pool config { private static final logger logger = loger factory . get logger(threadpoolconfig . class);@ bean public executor global executor(){//获取当前cpu核心号intcpunum = runtime.getruntime()。可用处理器();threadpooltasktexecutor pool executor = new threadpooltasktexecutor();pool executor . setcorepoolsize(cpuNum);//配置最大线程数pooleexecutor . setmaxpoolsize(CPU num * 2);//配置队列大小pooleexecutor . setqueuecapacity(300);//线程生存期pooleexecutor . setkeepaliveseconds(60);//配置名称前缀pooleuxecutor。线程池中线程的SethreadNamePrefix(“全局执行器”);// CALLER_RUNS:不是在新线程中执行任务,而是有调用者所在的线程执行pooleuxecutionhandler(新线程pooleexecutor . callerrunpolicy());//执行初始化pool executor . initialize();logger.info(LogConstant。旗帜);logger . info(log constant . log _ success _ prefix+" Spring全局线程池初始化完成...");logger . info(JSON . tojsonstring(pool executor));logger.info(LogConstant。旗帜);返回池执行程序;}}使用/* * *全局线程池配置test */@ async(" global executor ")@ override public void global executor(){ logger . info(" test thread!");} Idea热部署插件:Jrebel Idea社区版可以满足通用开发,支持maven和gradle项目。真的没必要破解Idea,哈哈哈哈哈哈哈。
使用特Nice基本不需要重启服务,网上有注册码。
初始化映射约定Map Map two = new HashMap();mapTwo.put("a "," b ");mapTwo.put("c "," d ");8 Java 8新特性:双括号初始化mapmapone = New hashmap(){ { put(" one "," testone ");put("two "," test two ");put("three "," test three ");}};com.google.guava中的方法不可变map =不可变map . of(" a ",1," b ",2," c ",3);logger . info(string . value of(map));初始化列表构造List,add() List listOne = new ArrayList
评论前必须登录!
注册