├── src ├── main │ ├── resources │ │ ├── application-prod.properties │ │ ├── application.properties │ │ ├── application-dev.properties │ │ └── application-dev-github.properties │ └── java │ │ └── com │ │ ├── cmazxiaoma │ │ ├── cpucache │ │ │ ├── L1CacheMissFast.class │ │ │ ├── Test1.java │ │ │ ├── Test.java │ │ │ ├── L1CacheMissFast.java │ │ │ ├── L1CacheMiss1.java │ │ │ └── L1CacheMissSlow.java │ │ ├── Javaoffer │ │ │ ├── classLoader │ │ │ │ ├── Data.class │ │ │ │ ├── Data.java │ │ │ │ ├── ClassLoaderChecker.java │ │ │ │ └── MyClassLoader.java │ │ │ ├── Test.java │ │ │ └── 快速排序 │ │ │ │ └── QuckTest.java │ │ ├── 慕课网并发学习 │ │ │ ├── Mooc.java │ │ │ ├── JUC │ │ │ │ ├── ThisThread.java │ │ │ │ ├── ThreadJoinTest.java │ │ │ │ ├── ThreadInterruptedTest.java │ │ │ │ ├── ThreadInterruptTest.java │ │ │ │ └── UnsafeParkTest.java │ │ │ ├── 线程安全策略 │ │ │ │ ├── B.java │ │ │ │ ├── A.java │ │ │ │ ├── C.java │ │ │ │ └── VectorTest.java │ │ │ ├── 安全发布对象 │ │ │ │ ├── Publish.java │ │ │ │ ├── Singleton.java │ │ │ │ └── Escape.java │ │ │ └── Hystrix │ │ │ │ ├── HystrixConfig.java │ │ │ │ └── HystrixController1.java │ │ ├── event │ │ │ └── Test.java │ │ ├── game │ │ │ └── SparseArray.java │ │ ├── concurrent │ │ │ ├── Test.java │ │ │ ├── aqs │ │ │ │ ├── Person.java │ │ │ │ ├── PersonTest.java │ │ │ │ ├── Test1.java │ │ │ │ ├── Test3.java │ │ │ │ ├── LockSupportTest.java │ │ │ │ ├── Test2.java │ │ │ │ ├── Test.java │ │ │ │ └── MyLockTest2.java │ │ │ ├── lock │ │ │ │ ├── ILock.java │ │ │ │ ├── MyLock.java │ │ │ │ ├── MyLockTest.java │ │ │ │ ├── MyReentrantLockTest.java │ │ │ │ └── MyReentrantLock.java │ │ │ ├── nanos │ │ │ │ └── NanosTest.java │ │ │ ├── interrupt │ │ │ │ └── InterruptTest.java │ │ │ ├── CheckLockTest.java │ │ │ ├── CustomThreadPoolExecutor.java │ │ │ ├── ThreadTest.java │ │ │ ├── blockingqueue │ │ │ │ ├── PriorityTask.java │ │ │ │ └── ArrayBlockingQueueTest.java │ │ │ ├── MyThreadLocal.java │ │ │ ├── VolatileTest.java │ │ │ └── SemaphoreTest.java │ │ ├── elasticsearch │ │ │ ├── Added.java │ │ │ ├── Updated.java │ │ │ ├── Book.java │ │ │ ├── ResultVo.java │ │ │ ├── ElasticSearchConfig.java │ │ │ ├── ResultVoGenerator.java │ │ │ └── ResultCode.java │ │ ├── controller │ │ │ ├── test │ │ │ │ ├── Son.java │ │ │ │ ├── ThreadLocalTest.java │ │ │ │ └── Parent.java │ │ │ ├── v1 │ │ │ │ ├── BaseController.java │ │ │ │ └── Test.java │ │ │ ├── handlerMethod │ │ │ │ └── GetController.java │ │ │ ├── v2 │ │ │ │ ├── BaseController1.java │ │ │ │ └── Test.java │ │ │ └── ThreadController.java │ │ ├── quzhuanxiang │ │ │ ├── lei │ │ │ │ ├── A.java │ │ │ │ └── B.java │ │ │ ├── Test1.java │ │ │ ├── Test3.java │ │ │ ├── ConsumerDemo2.java │ │ │ ├── Test2.java │ │ │ ├── Test.java │ │ │ └── GameBoxActivityConstant.java │ │ ├── redis │ │ │ ├── KeyPrefix.java │ │ │ ├── IdUtil.java │ │ │ ├── SeckillKeyPrefix.java │ │ │ ├── BasePrefix.java │ │ │ └── RedisConfig.java │ │ ├── spring │ │ │ ├── multipleBeans │ │ │ │ ├── IBeanService.java │ │ │ │ ├── BeanServiceImplV1.java │ │ │ │ └── BeanServiceImplV2.java │ │ │ ├── bean │ │ │ │ ├── SpringTest3.java │ │ │ │ ├── SpringTest1.java │ │ │ │ ├── SpringTest2.java │ │ │ │ └── SpringTestBean.java │ │ │ ├── inject │ │ │ │ ├── User.java │ │ │ │ ├── MyBaseDao.java │ │ │ │ ├── MySessionFactory.java │ │ │ │ ├── MyHibernateDaoSupport.java │ │ │ │ └── MyFactoryBean.java │ │ │ ├── beanpostprocessor │ │ │ │ ├── MyInjected.java │ │ │ │ ├── IAbService.java │ │ │ │ ├── MyVersion.java │ │ │ │ ├── AbServiceImplV1.java │ │ │ │ └── AbServiceImplV2.java │ │ │ ├── loop │ │ │ │ ├── LoopReferenceB.java │ │ │ │ └── LoopReferenceA.java │ │ │ ├── SpringConfiguration.java │ │ │ ├── MyInstantiationAwareBeanPostProcessor.java │ │ │ ├── CommonSpringTest.java │ │ │ ├── lazy │ │ │ │ └── Hero.java │ │ │ ├── SpringApplicationContext.java │ │ │ └── compoent │ │ │ │ └── SpringTest4.java │ │ ├── caiji │ │ │ ├── Action.java │ │ │ └── Product.java │ │ ├── jinzhi │ │ │ └── Test.java │ │ ├── hystrix │ │ │ ├── AdminDto.java │ │ │ ├── Admin.java │ │ │ ├── BlockThread.java │ │ │ ├── ShutdownThread.java │ │ │ ├── NonNullTest.java │ │ │ ├── LambdaTest.java │ │ │ ├── UserBatchCommand.java │ │ │ ├── HystrixController.java │ │ │ ├── Test1.java │ │ │ ├── UserCommand.java │ │ │ └── BeanCopierTest.java │ │ ├── alibaba │ │ │ ├── Handler.java │ │ │ ├── Test5.java │ │ │ ├── MyClientChannel.java │ │ │ ├── MyServerChannel.java │ │ │ ├── JiechengTest.java │ │ │ ├── Test4.java │ │ │ ├── 贪心算法 │ │ │ │ ├── TestTanxin.java │ │ │ │ ├── CoinChange.java │ │ │ │ └── TestDongtai.java │ │ │ ├── 归并排序 │ │ │ │ ├── MergeTest.java │ │ │ │ ├── SimpleMergeSort.java │ │ │ │ └── Merge.java │ │ │ ├── Test.java │ │ │ └── Test3.java │ │ ├── netty │ │ │ └── demo │ │ │ │ ├── read.me │ │ │ │ ├── oio │ │ │ │ ├── PlainOioClient.java │ │ │ │ └── PlainOioServer.java │ │ │ │ ├── aio │ │ │ │ ├── SimpleAioClient.java │ │ │ │ └── SimpleAioServer.java │ │ │ │ ├── EchoClinentHandler.java │ │ │ │ ├── juejin │ │ │ │ └── test │ │ │ │ │ └── ByteBufTest.java │ │ │ │ └── EchoServerHandler.java │ │ ├── hibernate │ │ │ ├── ISchoolDao.java │ │ │ └── IStudentDao.java │ │ ├── java │ │ │ ├── Test6.java │ │ │ ├── Test2.java │ │ │ ├── Test1.java │ │ │ ├── Test4.java │ │ │ ├── Test5.java │ │ │ └── Test3.java │ │ ├── model │ │ │ ├── Father.java │ │ │ └── Son.java │ │ ├── demo │ │ │ └── Test.java │ │ ├── lotteryTest │ │ │ ├── RandomTest.java │ │ │ └── Mall11LotteryResultVo.java │ │ ├── lambda │ │ │ ├── PhonePrdDto.java │ │ │ ├── Test1.java │ │ │ └── Test3.java │ │ ├── jdk8 │ │ │ └── Test.java │ │ ├── ClassTest.java │ │ ├── gc │ │ │ ├── RuntimeConsantPoolOOM.java │ │ │ ├── JavaVMStackSOF.java │ │ │ ├── MinorGC.java │ │ │ ├── ReferenceCountingGC.java │ │ │ ├── JavaVMStackOOM.java │ │ │ └── FinalizeEscapeGC.java │ │ ├── mybatis │ │ │ ├── ISchoolMapper.java │ │ │ └── IStudentMapper.java │ │ ├── service │ │ │ ├── IStudentService.java │ │ │ ├── StudentTestService.java │ │ │ └── impl │ │ │ │ └── StudentServiceImpl.java │ │ ├── excel │ │ │ └── DemoTest.java │ │ ├── yunshuanfu │ │ │ └── Test.java │ │ ├── WebApplication.java │ │ ├── DemoApplication.java │ │ ├── coupon │ │ │ └── CouponTextVo.java │ │ ├── Test.java │ │ ├── retry │ │ │ └── HttpController.java │ │ ├── cvte │ │ │ ├── InsertSort.java │ │ │ ├── SelectSort.java │ │ │ ├── BinarySort.java │ │ │ ├── BubbleSort.java │ │ │ └── MergeSort.java │ │ ├── date │ │ │ ├── LocalTimeTest1.java │ │ │ ├── ZoneTest.java │ │ │ └── ZeroTest.java │ │ ├── MyCommonDao.java │ │ ├── file │ │ │ └── FileTest.java │ │ ├── Test3.java │ │ ├── value │ │ │ └── ValueConfig.java │ │ ├── config │ │ │ └── FilterConfig.java │ │ ├── HibernateConfig.java │ │ └── extendsuper │ │ │ └── extendSuper.java │ │ ├── chengyu │ │ ├── ChallengeLevelTest.java │ │ └── Test.java │ │ ├── proxy │ │ ├── Dog.java │ │ ├── DogTest.java │ │ ├── DogFactory.java │ │ └── CglibProxy.java │ │ ├── Test3.java │ │ └── twozerotwozero │ │ └── RedLock.java └── test │ └── java │ └── com │ └── cmazxiaoma │ ├── InitSpringTest.java │ ├── value │ └── MyValueTest.java │ ├── model │ └── SonTest.java │ ├── string │ └── StringTest.java │ ├── beanpostprocessor │ └── MyVersionBeanPostProcessorTest.java │ ├── spring │ ├── LoopReferenceTest.java │ └── SpringTest.java │ ├── service │ └── impl │ │ ├── StudentServiceImplTest.java │ │ └── StudentTestServiceTest.java │ ├── linkedList │ ├── Node.java │ └── LinkedListCreator.java │ ├── hibernate │ └── MyCommonDaoTest.java │ ├── mybatis │ └── ISchoolMapperTest.java │ ├── thread │ └── ThreadTest.java │ └── redis │ └── RedisTest.java ├── .gitignore └── README.md /src/main/resources/application-prod.properties: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.profiles.active=dev -------------------------------------------------------------------------------- /src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmazxiaoma/framework-analysis/HEAD/src/main/resources/application-dev.properties -------------------------------------------------------------------------------- /src/main/resources/application-dev-github.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmazxiaoma/framework-analysis/HEAD/src/main/resources/application-dev-github.properties -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cpucache/L1CacheMissFast.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmazxiaoma/framework-analysis/HEAD/src/main/java/com/cmazxiaoma/cpucache/L1CacheMissFast.class -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/Javaoffer/classLoader/Data.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmazxiaoma/framework-analysis/HEAD/src/main/java/com/cmazxiaoma/Javaoffer/classLoader/Data.class -------------------------------------------------------------------------------- /src/main/java/com/chengyu/ChallengeLevelTest.java: -------------------------------------------------------------------------------- 1 | package com.chengyu; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2020/1/14 15:49 8 | */ 9 | public class ChallengeLevelTest { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/Mooc.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/14 4:11 8 | */ 9 | public class Mooc { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/Javaoffer/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.Javaoffer; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/18 21:44 8 | */ 9 | public class Test { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/event/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.event; 2 | 3 | /** 4 | * @author 事件发布订阅解耦 学习 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/5/21 11:38 8 | */ 9 | public class Test { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/game/SparseArray.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.game; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2020/1/13 11:05 8 | */ 9 | public class SparseArray { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/20 16:20 8 | */ 9 | public class Test { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/elasticsearch/Added.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.elasticsearch; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/10/24 17:07 8 | */ 9 | public interface Added { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/elasticsearch/Updated.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.elasticsearch; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/10/24 17:07 8 | */ 9 | public interface Updated { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/controller/test/Son.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.controller.test; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/4/17 23:19 8 | */ 9 | public class Son extends Parent{ 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/quzhuanxiang/lei/A.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.quzhuanxiang.lei; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/10/23 18:09 8 | */ 9 | public abstract class A { 10 | public int i = 1; 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/redis/KeyPrefix.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.redis; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/5/10 12:35 8 | */ 9 | public interface KeyPrefix { 10 | 11 | int expireSeconds(); 12 | 13 | String getPrefix(); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/multipleBeans/IBeanService.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.multipleBeans; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/24 17:56 8 | */ 9 | public interface IBeanService { 10 | 11 | void doSomething(); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/aqs/Person.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.aqs; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/9/11 15:29 8 | */ 9 | @FunctionalInterface 10 | public interface Person { 11 | void sayHello(String message); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/caiji/Action.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.caiji; 2 | 3 | import org.openqa.selenium.WebDriver; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/11/24 13:25 10 | */ 11 | public interface Action { 12 | 13 | String html(WebDriver webDriver); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/jinzhi/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.jinzhi; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/16 20:01 8 | */ 9 | public class Test { 10 | 11 | public static void main(String[] args) { 12 | System.out.println(~0); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/AdminDto.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/6/3 16:07 10 | */ 11 | @Data 12 | public class AdminDto { 13 | 14 | private Long id; 15 | private String name; 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/JUC/ThisThread.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.JUC; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/15 23:13 8 | */ 9 | public class ThisThread extends Thread { 10 | 11 | @Override 12 | public void run() { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/proxy/Dog.java: -------------------------------------------------------------------------------- 1 | package com.proxy; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2020/1/29 17:35 8 | */ 9 | public class Dog { 10 | 11 | public String call(String param) { 12 | System.out.println("dog:" + param); 13 | return param; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/线程安全策略/B.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.线程安全策略; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/14 4:54 8 | */ 9 | public class B extends A { 10 | 11 | @Override 12 | public void sb1() { 13 | super.sb1(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/lock/ILock.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.lock; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/17 17:03 8 | */ 9 | public interface ILock { 10 | 11 | void lock() throws InterruptedException; 12 | 13 | void unLock(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/quzhuanxiang/Test1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.quzhuanxiang; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/9/21 16:34 8 | */ 9 | public class Test1 { 10 | 11 | public static void main(String[] args) { 12 | System.out.println(0 <= 00.00d); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/Handler.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/28 14:09 8 | */ 9 | public interface Handler { 10 | 11 | void completed(K result, V attachment); 12 | 13 | void failed(Throwable ex, V attachment); 14 | } 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/netty/demo/read.me: -------------------------------------------------------------------------------- 1 | 进程中的IO调用步骤大致可以分为以下四步: 2 | 3 | 进程向操作系统请求数据 ; 4 | 5 | 操作系统把外部数据加载到内核的缓冲区中; 6 | 7 | 操作系统把内核的缓冲区拷贝到进程的缓冲区 ; 8 | 9 | 进程获得数据完成自己的功能 ; 10 | 11 | 当操作系统在把外部数据放到进程缓冲区的这段时间(即上述的第二,三步), 12 | 如果应用进程是挂起等待的,那么就是同步IO,反之,就是异步IO,也就是AIO 13 | 14 | 15 | DMA英文全称是Direct Memory Access,意思是直接存储器访问。 16 | DMA是指外部设备不通过CPU而直接与系统内存交换数据的接口技术。这样数据的传送速度就取决于存储器和外设的工作速度。 -------------------------------------------------------------------------------- /src/main/java/com/proxy/DogTest.java: -------------------------------------------------------------------------------- 1 | package com.proxy; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2020/1/29 17:41 8 | */ 9 | public class DogTest { 10 | 11 | public static void main(String[] args) { 12 | Dog dog = DogFactory.getInstance(new CglibProxy()); 13 | System.out.println(dog.call("123")); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/bean/SpringTest3.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.bean; 2 | 3 | import com.cmazxiaoma.spring.CommonSpringTest; 4 | import org.springframework.stereotype.Component; 5 | 6 | /** 7 | * @author cmazxiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2018/8/8 16:16 11 | */ 12 | public class SpringTest3 extends CommonSpringTest { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/JUC/ThreadJoinTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.JUC; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/15 23:32 8 | */ 9 | public class ThreadJoinTest { 10 | 11 | public static void main(String[] args) throws InterruptedException { 12 | Thread.currentThread().join(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/线程安全策略/A.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.线程安全策略; 2 | 3 | import com.google.common.collect.Maps; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/2/14 4:54 10 | */ 11 | public class A { 12 | 13 | public final void sb() { 14 | 15 | } 16 | 17 | public void sb1() { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hibernate/ISchoolDao.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hibernate; 2 | 3 | import com.cmazxiaoma.model.School; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author cmazxiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2018/7/3 17:56 11 | */ 12 | public interface ISchoolDao extends JpaRepository { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/Admin.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | import lombok.Data; 4 | import lombok.experimental.Accessors; 5 | 6 | /** 7 | * @author xiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2019/6/3 16:01 11 | */ 12 | @Data 13 | @Accessors(chain = true) 14 | public class Admin { 15 | 16 | private Long id; 17 | private String name; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/inject/User.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.inject; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * @author xiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2019/4/10 14:49 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | public class User { 15 | 16 | private Long id; 17 | private String name; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hibernate/IStudentDao.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hibernate; 2 | 3 | import com.cmazxiaoma.model.Student; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author cmazxiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2018/7/3 17:56 11 | */ 12 | public interface IStudentDao extends JpaRepository { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/Test5.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba; 2 | 3 | import org.assertj.core.util.Lists; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author xiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2020/1/10 10:47 12 | */ 13 | public class Test5 { 14 | 15 | public static void main(String[] args) { 16 | System.out.println(2&1); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/beanpostprocessor/MyInjected.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.beanpostprocessor; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/8/13 13:05 10 | */ 11 | @Documented 12 | @Target({ElementType.FIELD}) 13 | @Retention(RetentionPolicy.RUNTIME) 14 | public @interface MyInjected { 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/Javaoffer/classLoader/Data.java: -------------------------------------------------------------------------------- 1 | //package com.cmazxiaoma.Javaoffer.classLoader; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/18 22:18 8 | */ 9 | public class Data { 10 | 11 | static { 12 | System.out.println("load Data class"); 13 | } 14 | 15 | public static void main(String[] args) { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/java/Test6.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.java; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/16 23:34 8 | */ 9 | public class Test6 { 10 | 11 | static { 12 | a = 7; 13 | } 14 | 15 | static int a = 2; 16 | 17 | public static void main(String[] args) { 18 | System.out.println(a); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/model/Father.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.model; 2 | 3 | import lombok.EqualsAndHashCode; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2018/7/4 18:13 12 | */ 13 | @Getter 14 | @Setter 15 | @EqualsAndHashCode 16 | public class Father { 17 | 18 | protected String fatherName; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/nanos/NanosTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.nanos; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/8/22 20:05 10 | */ 11 | public class NanosTest { 12 | 13 | public static void main(String[] args) { 14 | System.out.println(TimeUnit.SECONDS.toNanos(1)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/java/Test2.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.java; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/16 23:14 8 | */ 9 | public class Test2 { 10 | 11 | static { 12 | a = 6; 13 | } 14 | 15 | public static int a = 9; 16 | 17 | public static void main(String[] args) { 18 | System.out.println(Test2.a); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/aqs/PersonTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.aqs; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/9/11 15:29 8 | */ 9 | public class PersonTest { 10 | 11 | public static void main(String[] args) { 12 | Person person = message -> System.out.println("say Hello:" + message); 13 | person.sayHello("123"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/aqs/Test1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.aqs; 2 | 3 | import java.util.concurrent.ThreadLocalRandom; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/12/7 15:26 10 | */ 11 | public class Test1 { 12 | 13 | public static void main(String[] args) { 14 | System.out.println(ThreadLocalRandom.current().nextInt(1, 101)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/multipleBeans/BeanServiceImplV1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.multipleBeans; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/8/24 17:57 10 | */ 11 | @Service 12 | public class BeanServiceImplV1 implements IBeanService { 13 | 14 | @Override 15 | public void doSomething() { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/multipleBeans/BeanServiceImplV2.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.multipleBeans; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/8/24 17:58 10 | */ 11 | @Service 12 | public class BeanServiceImplV2 implements IBeanService { 13 | 14 | @Override 15 | public void doSomething() { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/quzhuanxiang/lei/B.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.quzhuanxiang.lei; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/10/23 18:10 8 | */ 9 | public class B extends A { 10 | 11 | public B() { 12 | super(); 13 | i = 2; 14 | } 15 | 16 | public static void main(String[] args) { 17 | B b = new B(); 18 | System.out.println(b.i); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/InitSpringTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | 9 | @SpringBootTest 10 | @RunWith(SpringRunner.class) 11 | public class InitSpringTest { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/model/Son.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.model; 2 | 3 | import lombok.Data; 4 | import lombok.EqualsAndHashCode; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | /** 9 | * @author cmazxiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2018/7/4 18:13 13 | */ 14 | @Getter 15 | @Setter 16 | @EqualsAndHashCode(callSuper = true) 17 | public class Son extends Father { 18 | 19 | private String sonName; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/demo/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.demo; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author xiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2019/12/20 14:49 12 | */ 13 | public class Test { 14 | 15 | public static void main(String[] args) { 16 | List limitResult = Lists.newArrayList(); 17 | System.out.println("limit:" + limitResult); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/Test3.java: -------------------------------------------------------------------------------- 1 | package com; 2 | 3 | import com.cmazxiaoma.concurrent.MyThreadLocal; 4 | 5 | import java.text.ParseException; 6 | import java.time.LocalDateTime; 7 | import java.util.Date; 8 | 9 | /** 10 | * @author xiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2019/6/4 11:36 14 | */ 15 | public class Test3 { 16 | 17 | public static void main(String[] args) throws ParseException { 18 | System.out.println(Short.valueOf("600")); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/redis/IdUtil.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.redis; 2 | 3 | import java.util.UUID; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/8/30 6:46 10 | */ 11 | public class IdUtil { 12 | 13 | public static String gen() { 14 | return UUID.randomUUID().toString().replace("-", ""); 15 | } 16 | 17 | public static void main(String[] args) { 18 | System.out.println(IdUtil.gen()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/redis/SeckillKeyPrefix.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.redis; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/28 9:35 8 | */ 9 | public class SeckillKeyPrefix extends BasePrefix { 10 | 11 | public SeckillKeyPrefix(int expireSeconds, String prefix) { 12 | super(expireSeconds, prefix); 13 | } 14 | 15 | public static SeckillKeyPrefix seckillKeyPrefix = new SeckillKeyPrefix(0, "seckill"); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/lotteryTest/RandomTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.lotteryTest; 2 | 3 | import java.util.concurrent.ThreadLocalRandom; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/10/24 20:11 10 | */ 11 | public class RandomTest { 12 | 13 | public static void main(String[] args) { 14 | for (int i = 0; i < 1000; i++) { 15 | System.out.println((int) ThreadLocalRandom.current().nextDouble(3, 4)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/beanpostprocessor/IAbService.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.beanpostprocessor; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/13 13:11 8 | */ 9 | @MyVersion("V1") 10 | public interface IAbService { 11 | 12 | @MyVersion("V1") 13 | void create(); 14 | 15 | @MyVersion("V2") 16 | void retrieve(); 17 | 18 | @MyVersion("V1") 19 | void update(); 20 | 21 | @MyVersion("V1") 22 | void delete(); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/inject/MyBaseDao.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 文件名:MyBaseDao.java
3 | * 描述:
4 | * 开发人员:wutao
5 | * 创建时间: 2018-9-26 6 | */ 7 | 8 | package com.cmazxiaoma.spring.inject; 9 | 10 | import org.springframework.context.annotation.Lazy; 11 | import org.springframework.stereotype.Component; 12 | 13 | /** 14 | * 类名: MyBaseDao
15 | * 描述:
16 | * 开发人员: wutao
17 | * 创建时间: 2018-9-26 18 | */ 19 | @Component 20 | public class MyBaseDao extends MyHibernateDaoSupport { 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/loop/LoopReferenceB.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.loop; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Scope; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2018/8/14 15:43 12 | */ 13 | @Component 14 | public class LoopReferenceB { 15 | 16 | @Autowired 17 | private LoopReferenceA loopReferenceA; 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/lambda/PhonePrdDto.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.lambda; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author xiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2020/1/7 17:55 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | public class PhonePrdDto implements Serializable { 17 | 18 | // 设备号 19 | private String phoneId; 20 | // 产品号 21 | private String prdId; 22 | 23 | private Long userId; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/jdk8/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.jdk8; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/10/24 20:42 10 | */ 11 | public class Test { 12 | 13 | public static void main(String[] args) { 14 | String s = " "; 15 | 16 | System.out.println(StringUtils.isNotBlank(s)); 17 | System.out.println(!StringUtils.isBlank(s)); 18 | System.out.println(!StringUtils.isEmpty(s)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/beanpostprocessor/MyVersion.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.beanpostprocessor; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import java.lang.annotation.*; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2018/8/13 13:07 12 | */ 13 | @Documented 14 | @Retention(RetentionPolicy.RUNTIME) 15 | @Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) 16 | public @interface MyVersion { 17 | 18 | String value() default ""; 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/aqs/Test3.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.aqs; 2 | 3 | import java.text.DecimalFormat; 4 | import java.util.Calendar; 5 | 6 | /** 7 | * @author xiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2019/12/8 1:11 11 | */ 12 | public class Test3 { 13 | 14 | public static void main(String[] args) { 15 | // DecimalFormat df1 = new DecimalFormat("0.00"); 16 | DecimalFormat df =new DecimalFormat("000000000"); 17 | System.out.println(df.format(110757970)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/inject/MySessionFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 文件名:MySessionFactory.java
3 | * 描述:
4 | * 开发人员:wutao
5 | * 创建时间: 2018-9-26 6 | */ 7 | 8 | package com.cmazxiaoma.spring.inject; 9 | 10 | import org.springframework.stereotype.Component; 11 | 12 | /** 13 | * 类名: MySessionFactory
14 | * 描述:
15 | * 开发人员: wutao
16 | * 创建时间: 2018-9-26 17 | */ 18 | @Component 19 | public class MySessionFactory { 20 | 21 | public String getName() { 22 | return "MySessionFactory"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/proxy/DogFactory.java: -------------------------------------------------------------------------------- 1 | package com.proxy; 2 | 3 | import org.mockito.cglib.proxy.Enhancer; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2020/1/29 17:39 10 | */ 11 | public class DogFactory { 12 | 13 | public static Dog getInstance(CglibProxy cglibProxy) { 14 | Enhancer enhancer = new Enhancer(); 15 | enhancer.setSuperclass(Dog.class); 16 | enhancer.setCallback(cglibProxy); 17 | Dog dog = (Dog) enhancer.create(); 18 | return dog; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/ClassTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma; 2 | 3 | import com.cmazxiaoma.慕课网并发学习.线程安全策略.A; 4 | import com.cmazxiaoma.慕课网并发学习.线程安全策略.B; 5 | 6 | /** 7 | * @author xiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2019/3/22 17:47 11 | */ 12 | public class ClassTest { 13 | 14 | public static void main(String[] args) { 15 | System.out.println(A.class.isInstance(new A())); 16 | System.out.println(B.class.isInstance(new A())); 17 | System.out.println(A.class.isInstance(new B())); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/gc/RuntimeConsantPoolOOM.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.gc; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/12/4 18:08 8 | */ 9 | public class RuntimeConsantPoolOOM { 10 | 11 | public static void main(String[] args) { 12 | String str1 = new StringBuffer("计算机").append("软件").toString(); 13 | System.out.println(str1.intern() == str1); 14 | 15 | String str2 = new StringBuffer("ja1").append("va").toString(); 16 | System.out.println(str2.intern() == str2); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/安全发布对象/Publish.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.安全发布对象; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/14 4:12 8 | */ 9 | public class Publish { 10 | 11 | private String[] state = {"c", "m", "a", "z"}; 12 | 13 | 14 | public String[] getStates() { 15 | return state; 16 | } 17 | 18 | public static void main(String[] args) { 19 | Publish publish = new Publish(); 20 | publish.getStates()[0] = "a"; 21 | publish.getStates()[0] = "b"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/value/MyValueTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.value; 2 | 3 | import com.cmazxiaoma.InitSpringTest; 4 | import org.junit.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | 7 | /** 8 | * @author xiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2019/5/16 10:40 12 | */ 13 | public class MyValueTest extends InitSpringTest { 14 | 15 | @Autowired 16 | private ValueConfig valueConfig; 17 | 18 | @Test 19 | public void test() { 20 | System.out.println(valueConfig.getList().toString()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/BlockThread.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/5/31 20:50 10 | */ 11 | public class BlockThread implements Runnable { 12 | 13 | @Override 14 | public void run() { 15 | try { 16 | TimeUnit.SECONDS.sleep(5); 17 | System.out.println("=============阻塞执行完毕...================"); 18 | } catch (InterruptedException e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/interrupt/InterruptTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.interrupt; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/21 11:38 8 | */ 9 | public class InterruptTest { 10 | 11 | public static void main(String[] args) { 12 | Thread wt = Thread.currentThread(); 13 | 14 | System.out.println(wt.isInterrupted()); 15 | System.out.println(wt.isInterrupted()); 16 | 17 | System.out.println(Thread.interrupted()); 18 | System.out.println(Thread.interrupted()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/JUC/ThreadInterruptedTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.JUC; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/15 16:15 8 | */ 9 | public class ThreadInterruptedTest { 10 | 11 | /** 12 | * 检测线程中断 13 | * @param args 14 | */ 15 | public static void main(String[] args) { 16 | System.out.println(Thread.interrupted()); 17 | Thread.currentThread().interrupt(); 18 | System.out.println(Thread.interrupted()); 19 | System.out.println(Thread.interrupted()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/model/SonTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.model; 2 | 3 | import org.junit.Test; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/7/4 18:17 10 | */ 11 | public class SonTest { 12 | 13 | @Test 14 | public void test() { 15 | Son son1 = new Son(); 16 | son1.setSonName("son1"); 17 | son1.setFatherName("baseFather"); 18 | 19 | Son son2 = new Son(); 20 | son2.setSonName("son1"); 21 | son2.setFatherName("baseFather2"); 22 | 23 | System.out.println(son1.equals(son2)); 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/MyClientChannel.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.nio.channels.CompletionHandler; 5 | 6 | /** 7 | * @author cmazxiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2019/2/28 14:19 11 | */ 12 | public class MyClientChannel { 13 | 14 | public final void read(ByteBuffer dst, 15 | A attachment, 16 | Handler handler) 17 | { 18 | System.out.println("read"); 19 | 20 | handler.completed(1, attachment); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/lambda/Test1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.lambda; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * @author xiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2019/3/19 15:17 12 | */ 13 | public class Test1 { 14 | 15 | public static void main(String[] args) { 16 | List ab = new ArrayList<>(); 17 | ab.add("aaa"); 18 | ab.add("aaa"); 19 | ab.add("bbbb"); 20 | System.out.println(ab.parallelStream().map(p -> p).collect(Collectors.toList()).toString()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/java/Test1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.java; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/16 23:08 8 | */ 9 | public class Test1 { 10 | 11 | public static void main(String[] args) { 12 | new Test1(); 13 | } 14 | 15 | { 16 | int a = 6; 17 | if (a > 4) { 18 | System.out.println("test初始化块,局部变量a的值大于4"); 19 | } 20 | } 21 | 22 | { 23 | System.out.println("test类的第二个初始化块"); 24 | } 25 | 26 | public Test1() { 27 | System.out.println("test类的无参数化构造器"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/mybatis/ISchoolMapper.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.mybatis; 2 | 3 | import com.cmazxiaoma.model.School; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Param; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2018/7/5 13:49 15 | */ 16 | @Mapper 17 | public interface ISchoolMapper { 18 | 19 | List findOneByV1(@Param("schoolId") String schoolId); 20 | 21 | School findOneByV2(@Param("schoolId") String schoolId); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/CheckLockTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/12/27 13:32 8 | */ 9 | public class CheckLockTest { 10 | 11 | private static final Object object = new Object(); 12 | 13 | public static void main(String[] args) { 14 | 15 | synchronized (object) { 16 | // 判断是否获取object锁 17 | System.out.println("get lock result:" + Thread.holdsLock(object)); 18 | } 19 | 20 | System.out.println("get lock result:" + Thread.holdsLock(object)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/service/IStudentService.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.service; 2 | 3 | import com.cmazxiaoma.model.Student; 4 | import org.apache.ibatis.annotations.Param; 5 | 6 | /** 7 | * @author cmazxiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2018/7/6 15:47 11 | */ 12 | public interface IStudentService { 13 | 14 | Student findOneByV1(@Param("studentId") String studentId); 15 | 16 | Student findOneByV2(@Param("studentId") String studentId); 17 | 18 | Student findOneByV3(@Param("studentId") String studentId); 19 | 20 | int updateIsDel(@Param("studentId") String studentId); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/lock/MyLock.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.lock; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/17 17:02 8 | */ 9 | public class MyLock implements ILock { 10 | 11 | private boolean isLooked = false; 12 | 13 | @Override 14 | public synchronized void lock() throws InterruptedException { 15 | while (isLooked) { 16 | wait(); 17 | } 18 | isLooked = true; 19 | } 20 | 21 | @Override 22 | public synchronized void unLock() { 23 | isLooked = false; 24 | notify(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/ShutdownThread.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | import org.springframework.util.StopWatch; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/5/31 20:17 10 | */ 11 | public class ShutdownThread implements Runnable { 12 | 13 | private StopWatch stopWatch; 14 | 15 | public ShutdownThread(StopWatch stopWatch) { 16 | this.stopWatch = stopWatch; 17 | } 18 | 19 | @Override 20 | public void run() { 21 | stopWatch.stop(); 22 | System.out.println("============耗时:" + stopWatch.getTotalTimeSeconds()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/MyServerChannel.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.nio.channels.CompletionHandler; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2019/2/28 14:10 12 | */ 13 | public class MyServerChannel { 14 | 15 | public void accept(A attachment, Handler handler) { 16 | System.out.println("accept"); 17 | MyClientChannel myClientChannel = new MyClientChannel(); 18 | handler.completed(myClientChannel, attachment); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/excel/DemoTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.excel; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author xiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2019/10/24 10:01 12 | */ 13 | public class DemoTest { 14 | 15 | public static void main(String[] args) { 16 | List resultList = Lists.newArrayList(); 17 | 18 | for (int i = 1; i <=66; i++) { 19 | resultList.add(i); 20 | } 21 | 22 | System.out.println(resultList.size()); 23 | 24 | System.out.println(resultList.subList(0, 65)); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/bean/SpringTest1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.bean; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.stereotype.Service; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2018/8/8 16:13 12 | */ 13 | 14 | public class SpringTest1 { 15 | 16 | private String name = "springTest1"; 17 | 18 | @Override 19 | public String toString() { 20 | return "SpringTest1{" + 21 | "name='" + name + '\'' + 22 | '}'; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/lock/MyLockTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.lock; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/17 17:08 8 | */ 9 | public class MyLockTest { 10 | 11 | private MyLock myLock = new MyLock(); 12 | 13 | public static void main(String[] args) throws InterruptedException { 14 | MyLockTest myLockTest = new MyLockTest(); 15 | myLockTest.myLock.lock(); 16 | myLockTest.tryLock(); 17 | myLockTest.myLock.unLock(); 18 | } 19 | 20 | public void tryLock() throws InterruptedException { 21 | myLock.lock(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/SpringConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring; 2 | 3 | import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2018/8/8 16:59 12 | */ 13 | @Configuration 14 | public class SpringConfiguration { 15 | 16 | @Bean 17 | public BeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor() { 18 | return new MyBeanDefinitionRegistryPostProcessor(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/aqs/LockSupportTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.aqs; 2 | 3 | import java.util.concurrent.locks.LockSupport; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2020/2/13 21:59 10 | */ 11 | public class LockSupportTest { 12 | 13 | public static void main(String[] args) { 14 | Thread t = new Thread(new Runnable() { 15 | public void run() { 16 | System.out.println("thread..."); 17 | LockSupport.park(); 18 | System.out.println("thread done."); 19 | } 20 | }); 21 | 22 | t.start(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cpucache/Test1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.cpucache; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/26 22:41 8 | */ 9 | public class Test1 { 10 | 11 | public static final long ITERATIONS = 500L * 1000L * 1000L; 12 | 13 | public static void main(String[] args) { 14 | System.out.println("iterations=" + ITERATIONS); 15 | 16 | int i = 2; 17 | 18 | while (--i != 0) { 19 | System.out.println("--i while"); 20 | } 21 | 22 | i = 2; 23 | 24 | while (i-- != 0) { 25 | System.out.println("i-- while"); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/线程安全策略/C.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.线程安全策略; 2 | 3 | import com.google.common.collect.Maps; 4 | 5 | import java.util.Collections; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | /** 10 | * @author cmazxiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2019/2/14 5:02 14 | */ 15 | public class C { 16 | 17 | public static void main(String[] args) { 18 | Map hashMap = Maps.newHashMap(); 19 | hashMap.put("boy", "cmazxiaoma"); 20 | 21 | Map newHashMap = Collections.unmodifiableMap(hashMap); 22 | newHashMap.put("girl", "doudou"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/安全发布对象/Singleton.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.安全发布对象; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/14 4:32 8 | */ 9 | public class Singleton { 10 | 11 | private volatile static Singleton instance = null; 12 | 13 | private Singleton() { 14 | 15 | } 16 | 17 | public static Singleton getInstance() { 18 | if (instance == null) { 19 | synchronized (Singleton.class) { 20 | if (instance == null) { 21 | instance = new Singleton(); 22 | } 23 | } 24 | } 25 | return instance; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/yunshuanfu/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.yunshuanfu; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/17 11:55 8 | */ 9 | public class Test { 10 | 11 | 12 | public static void main(String[] args) { 13 | 14 | if (!isFlag()) { 15 | System.out.println("false"); 16 | } 17 | 18 | if (isFlag() && print()) { 19 | System.out.println("gg"); 20 | } 21 | 22 | } 23 | 24 | public static boolean isFlag() { 25 | return false; 26 | } 27 | 28 | public static boolean print() { 29 | System.out.println("123"); 30 | return true; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/quzhuanxiang/Test3.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.quzhuanxiang; 2 | 3 | import com.google.common.collect.Maps; 4 | 5 | import java.util.Map; 6 | 7 | /** 8 | * @author xiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2019/9/26 18:07 12 | */ 13 | public class Test3 { 14 | 15 | public static void main(String[] args) { 16 | Map resultMap = Maps.newHashMapWithExpectedSize(6); 17 | for (int i = 0; i < 1000; i++) { 18 | resultMap.put(i, i); 19 | } 20 | System.out.println(resultMap); 21 | 22 | Long a = 666L; 23 | Long b = 66L; 24 | System.out.println(a.compareTo(b) >= 0L); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/loop/LoopReferenceA.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.loop; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Scope; 5 | import org.springframework.stereotype.Component; 6 | 7 | import javax.annotation.Resource; 8 | 9 | /** 10 | * @author cmazxiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2018/8/14 15:42 14 | */ 15 | @Component 16 | public class LoopReferenceA { 17 | 18 | @Autowired 19 | private LoopReferenceB loopReferenceB; 20 | 21 | // public LoopReferenceA(@Autowired LoopReferenceB loopReferenceB) { 22 | // this.loopReferenceB = loopReferenceB; 23 | // } 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/string/StringTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.string; 2 | 3 | import org.springframework.util.ReflectionUtils; 4 | 5 | import java.lang.reflect.Field; 6 | 7 | /** 8 | * @author xiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2019/4/18 22:13 12 | */ 13 | public class StringTest { 14 | 15 | public static void main(String[] args) throws IllegalAccessException { 16 | String s = new String("hello world"); 17 | Field field = ReflectionUtils.findField(String.class, "value"); 18 | ReflectionUtils.makeAccessible(field); 19 | char[] value = (char[]) field.get(s); 20 | value[5] = '_'; 21 | System.out.println(s); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/NonNullTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | import lombok.NonNull; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/5/27 14:28 10 | */ 11 | public class NonNullTest { 12 | 13 | public static void main(String[] args) { 14 | hello(null); 15 | } 16 | 17 | 18 | /** 19 | * 如果msg为空的话, 会直接在hello方法中进行抛出,而不会继续执行到hello1方法 20 | * @param msg 21 | */ 22 | public static void hello(@NonNull String msg) { 23 | hello1(msg); 24 | } 25 | 26 | public static void hello1(String msg) { 27 | System.out.println("=====hello1=========="); 28 | System.out.println(msg); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/gc/JavaVMStackSOF.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.gc; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/28 13:18 8 | */ 9 | public class JavaVMStackSOF { 10 | 11 | private int stackLength = 1; 12 | 13 | public void stackLeak() { 14 | stackLength ++; 15 | stackLeak(); 16 | } 17 | 18 | public static void main(String[] args) { 19 | JavaVMStackSOF javaVMStackSOF = new JavaVMStackSOF(); 20 | 21 | try { 22 | javaVMStackSOF.stackLeak(); 23 | } catch (Throwable ex) { 24 | System.out.println("stack length:" + javaVMStackSOF.stackLength); 25 | throw ex; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/MyInstantiationAwareBeanPostProcessor.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring; 2 | 3 | import com.cmazxiaoma.spring.bean.SpringTest3; 4 | import org.springframework.beans.BeansException; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; 7 | import org.springframework.stereotype.Component; 8 | 9 | /** 10 | * @author xiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2019/3/31 15:46 14 | */ 15 | @Component 16 | public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter { 17 | 18 | @Autowired 19 | private SpringTest3 springTest3; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/bean/SpringTest2.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.bean; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.stereotype.Service; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2018/8/8 16:14 12 | */ 13 | 14 | public class SpringTest2 { 15 | 16 | private String name = "springTest2"; 17 | 18 | @Override 19 | public String toString() { 20 | return "SpringTest2{" + 21 | "name='" + name + '\'' + 22 | '}'; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/aqs/Test2.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.aqs; 2 | 3 | import org.apache.commons.lang.time.DateFormatUtils; 4 | import org.apache.commons.lang3.time.DateUtils; 5 | 6 | import java.text.ParseException; 7 | import java.util.Date; 8 | 9 | /** 10 | * @author xiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2019/12/8 0:13 14 | */ 15 | public class Test2 { 16 | 17 | public static void main(String[] args) throws ParseException { 18 | System.out.println(Long.valueOf("000512791")); 19 | 20 | Date time = DateUtils.parseDate("201912081021087", "yyyyMMddHHmmssSSS"); 21 | 22 | System.out.println(DateFormatUtils.format(time, "yyyy-MM-dd HH:mm:ss.SSS")); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/caiji/Product.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.caiji; 2 | 3 | import lombok.Data; 4 | import org.codehaus.groovy.runtime.StringGroovyMethods; 5 | import org.omg.CORBA.StringHolder; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | * @author xiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2019/11/24 0:08 14 | */ 15 | @Data 16 | public class Product implements Serializable { 17 | 18 | // 商品标题 19 | private String title; 20 | 21 | // 商品图片 22 | private String imgUrl; 23 | 24 | // 价格 25 | private String price; 26 | 27 | // 促销字段 28 | private String promoWords; 29 | 30 | // 评论 31 | private String commit; 32 | 33 | // 店铺名称 34 | private String shopName; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/controller/v1/BaseController.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.controller.v1; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.ui.ModelMap; 5 | 6 | import javax.annotation.Resource; 7 | import javax.servlet.annotation.WebListener; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import javax.servlet.http.HttpSession; 11 | 12 | /** 13 | * @author cmazxiaoma 14 | * @version V1.0 15 | * @Description: TODO 16 | * @date 2018/8/7 13:30 17 | */ 18 | public abstract class BaseController { 19 | 20 | @Autowired 21 | protected HttpSession httpSession; 22 | 23 | @Autowired 24 | protected HttpServletRequest request; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/LambdaTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | import java.util.Objects; 7 | import java.util.stream.Collectors; 8 | 9 | /** 10 | * @author xiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2019/5/31 22:05 14 | */ 15 | public class LambdaTest { 16 | 17 | public static void main(String[] args) { 18 | List idList = Lists.newArrayList(); 19 | idList.add(0); 20 | idList.add(0); 21 | idList.add(0); 22 | 23 | List newList = idList.stream().filter(item -> !Objects.equals(item, 0)) 24 | .collect(Collectors.toList()); 25 | System.out.println(newList); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/beanpostprocessor/MyVersionBeanPostProcessorTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.beanpostprocessor; 2 | 3 | import com.cmazxiaoma.InitSpringTest; 4 | import com.cmazxiaoma.spring.beanpostprocessor.IAbService; 5 | import com.cmazxiaoma.spring.beanpostprocessor.MyInjected; 6 | import org.junit.Test; 7 | 8 | /** 9 | * @author cmazxiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2018/8/13 14:32 13 | */ 14 | public class MyVersionBeanPostProcessorTest extends InitSpringTest { 15 | 16 | @MyInjected 17 | private IAbService abService; 18 | 19 | @Test 20 | public void test() { 21 | abService.create(); 22 | abService.retrieve(); 23 | abService.update(); 24 | abService.delete(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/lock/MyReentrantLockTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.lock; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/17 17:28 8 | */ 9 | public class MyReentrantLockTest { 10 | 11 | private MyReentrantLock myReentrantLock = new MyReentrantLock(); 12 | 13 | public static void main(String[] args) throws InterruptedException { 14 | MyReentrantLockTest test = new MyReentrantLockTest(); 15 | test.myReentrantLock.lock(); 16 | test.tryLock(); 17 | test.tryLock(); 18 | test.tryLock(); 19 | test.myReentrantLock.unLock(); 20 | } 21 | 22 | public void tryLock() throws InterruptedException { 23 | myReentrantLock.lock(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/gc/MinorGC.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.gc; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/11/13 15:58 10 | */ 11 | public class MinorGC implements Serializable { 12 | 13 | public static final int _1MB = 1024 * 1024; 14 | 15 | public static void testAllocation() { 16 | byte[] allocation1, allocation2, allocation3, allocation4; 17 | allocation1 = new byte[2 * _1MB]; 18 | allocation2 = new byte[2 * _1MB]; 19 | allocation3 = new byte[2 * _1MB]; 20 | 21 | // 出现一次Minor GC 22 | allocation4 = new byte[4 * _1MB]; 23 | } 24 | 25 | public static void main(String[] args) { 26 | testAllocation(); 27 | } 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/CommonSpringTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring; 2 | 3 | import com.cmazxiaoma.spring.bean.SpringTest1; 4 | import com.cmazxiaoma.spring.bean.SpringTest2; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2018/8/8 16:15 12 | */ 13 | public abstract class CommonSpringTest { 14 | 15 | @Autowired 16 | private SpringTest1 springTest1; 17 | 18 | @Autowired 19 | private SpringTest2 springTest2; 20 | 21 | @Override 22 | public String toString() { 23 | return "CommonSpringTest{" + 24 | "springTest1=" + springTest1 + 25 | ", springTest2=" + springTest2 + 26 | '}'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/spring/LoopReferenceTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring; 2 | 3 | import com.cmazxiaoma.InitSpringTest; 4 | import com.cmazxiaoma.spring.loop.LoopReferenceA; 5 | import com.cmazxiaoma.spring.loop.LoopReferenceB; 6 | import org.junit.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | 9 | /** 10 | * @author cmazxiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2018/8/14 15:46 14 | */ 15 | public class LoopReferenceTest extends InitSpringTest { 16 | 17 | @Autowired 18 | private LoopReferenceA loopReferenceA; 19 | 20 | @Autowired 21 | private LoopReferenceB loopReferenceB; 22 | 23 | @Test 24 | public void test() { 25 | System.out.println(loopReferenceA); 26 | System.out.println(loopReferenceB); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cpucache/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.cpucache; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/22 14:31 8 | */ 9 | public class Test { 10 | 11 | public static void main(String[] args) { 12 | 13 | String[] stringArray1 = new String[1]; 14 | 15 | String[] stringArray2 = stringArray1; 16 | 17 | stringArray2[0] = "mayday"; 18 | 19 | 20 | for (String s : stringArray1) { 21 | System.out.println(s); 22 | } 23 | 24 | System.out.println("==============="); 25 | 26 | for (String s : stringArray2) { 27 | System.out.println(s); 28 | } 29 | 30 | for (String s : args) { 31 | System.out.println("arg=" + s); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/beanpostprocessor/AbServiceImplV1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.beanpostprocessor; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/8/13 15:52 10 | */ 11 | @Service 12 | public class AbServiceImplV1 implements IAbService { 13 | 14 | @Override 15 | public void create() { 16 | System.out.println("create v1"); 17 | } 18 | 19 | @Override 20 | public void retrieve() { 21 | System.out.println("retrieve v1"); 22 | } 23 | 24 | @Override 25 | public void update() { 26 | System.out.println("update v1"); 27 | } 28 | 29 | @Override 30 | public void delete() { 31 | System.out.println("delete v1"); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/beanpostprocessor/AbServiceImplV2.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.beanpostprocessor; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/8/13 15:53 10 | */ 11 | @Service 12 | public class AbServiceImplV2 implements IAbService { 13 | 14 | @Override 15 | public void create() { 16 | System.out.println("create v2"); 17 | } 18 | 19 | @Override 20 | public void retrieve() { 21 | System.out.println("retrieve v2"); 22 | } 23 | 24 | @Override 25 | public void update() { 26 | System.out.println("update v2"); 27 | } 28 | 29 | @Override 30 | public void delete() { 31 | System.out.println("delete v2"); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/gc/ReferenceCountingGC.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.gc; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/12 10:42 8 | */ 9 | public class ReferenceCountingGC { 10 | 11 | public Object instance = null; 12 | private static final int _1MB = 1024 * 1024; 13 | private static final byte[] bigSize = new byte[2 * _1MB]; 14 | 15 | public static void test() { 16 | ReferenceCountingGC objA = new ReferenceCountingGC(); 17 | ReferenceCountingGC objB = new ReferenceCountingGC(); 18 | objA.instance = objB; 19 | objB.instance = objA; 20 | objA = null; 21 | objB = null; 22 | System.gc(); 23 | } 24 | 25 | 26 | public static void main(String[] args) { 27 | test(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### read 2 | 一开始这个项目的目的是对Hibernate、MyBatis出现的问题进行归纳,但是我在总结问题的时候,陆陆续续发现了自己的不足。 3 | 于是乎到现在的Hibernate、MyBatis、Spring、Spring MVC、AQS、ThreadPoolExecutor、CountDownLatch使用场景和核心源码分析。 4 | 感觉自己还是真的菜鸡,有太多框架的底层实现都不怎么了解。 5 | 当山头被一座一座攻克时,你会发现掉到一个大坑中,因为不懂的领域的实在太多。 6 | 7 | #### 1.[使用Hibernate、JPA、Lombok遇到的有趣问题](https://www.jianshu.com/p/61d4e28ee254) 8 | #### 2.[看ThreadPoolExecutor源码前的骚操作](https://www.jianshu.com/p/4b94b7ae684b) 9 | #### 3.[JUC之CountDownLatch的源码和使用场景分析](https://www.jianshu.com/p/fa496f7bc553) 10 | #### 4.[通过循环引用问题来分析Spring源码](https://www.jianshu.com/p/768525bf9f30) 11 | #### 5.[SpringMVC中出现的线程安全问题分析](https://www.jianshu.com/p/672a8ecada6c) 12 | 13 | 还有在这个项目之前写的关于SpringMVC核心源码分析 14 | #### 1.[你真的会用HttpMessageConverter吗?](https://www.jianshu.com/p/0aaeb4144489) 15 | #### 2.[你真的了解Spring MVC处理请求流程吗?](https://www.jianshu.com/p/6f841d81ed72) 16 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/WebApplication.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.builder.SpringApplicationBuilder; 5 | import org.springframework.boot.web.support.SpringBootServletInitializer; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | /** 9 | * @author cmazxiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2018/7/4 16:22 13 | */ 14 | //@SpringBootApplication 15 | //@Configuration 16 | //public class WebApplication extends SpringBootServletInitializer { 17 | // 18 | // @Override 19 | // protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 20 | // // 设置启动类,用于独立tomcat运行的入口 21 | // return builder.sources(WebApplication.class); 22 | // } 23 | //} 24 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/mybatis/IStudentMapper.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.mybatis; 2 | 3 | import com.cmazxiaoma.model.School; 4 | import com.cmazxiaoma.model.Student; 5 | import org.apache.ibatis.annotations.Mapper; 6 | import org.apache.ibatis.annotations.Param; 7 | import org.apache.ibatis.annotations.Select; 8 | import org.springframework.stereotype.Repository; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2018/7/5 13:49 15 | */ 16 | @Mapper 17 | public interface IStudentMapper { 18 | 19 | Student findOneByV1(@Param("studentId") String studentId); 20 | 21 | Student findOneByV2(@Param("studentId") String studentId); 22 | 23 | Student findOneByV3(@Param("studentId") String studentId); 24 | 25 | int updateIsDel(@Param("studentId") String studentId); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/gc/JavaVMStackOOM.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.gc; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/28 13:25 8 | */ 9 | public class JavaVMStackOOM { 10 | 11 | private void dontStop() { 12 | while (true) { 13 | 14 | } 15 | } 16 | 17 | public void stackLeakByThread() { 18 | while (true) { 19 | Thread thread = new Thread(new Runnable() { 20 | @Override 21 | public void run() { 22 | dontStop(); 23 | } 24 | }); 25 | thread.start(); 26 | } 27 | } 28 | 29 | public static void main(String[] args) { 30 | JavaVMStackOOM javaVMStackOOM = new JavaVMStackOOM(); 31 | javaVMStackOOM.stackLeakByThread(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/java/Test4.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.java; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/16 23:25 8 | */ 9 | public class Test4 { 10 | 11 | static { 12 | System.out.println(1); 13 | } 14 | 15 | static { 16 | System.out.println(2); 17 | } 18 | 19 | { 20 | System.out.println(3); 21 | } 22 | 23 | public Test4() { 24 | System.out.println(4); 25 | } 26 | 27 | { 28 | System.out.println(5); 29 | } 30 | 31 | static { 32 | System.out.println(7); 33 | } 34 | 35 | public static Test4 test4 = new Test4(); 36 | 37 | public static void main(String[] args) { 38 | System.out.println(6); 39 | new Test4(); 40 | 41 | //1 2 7 3 5 4 6 3 5 4 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/lazy/Hero.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.lazy; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.beans.factory.config.BeanPostProcessor; 5 | import org.springframework.context.annotation.Lazy; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author cmazxiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2018/8/9 14:14 13 | */ 14 | @Component 15 | @Lazy 16 | public class Hero { 17 | 18 | public void qSkill() { 19 | System.out.println("q skill"); 20 | } 21 | 22 | public void wSkill() { 23 | System.out.println("w skill"); 24 | } 25 | 26 | public void eSkill() { 27 | System.out.println("e skill"); 28 | } 29 | 30 | public void rSkill() { 31 | System.out.println("r skill"); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/安全发布对象/Escape.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.安全发布对象; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/2/14 4:21 10 | */ 11 | public class Escape { 12 | 13 | private String name = null; 14 | 15 | public Escape() { 16 | new Thread(new MyThread()).start(); 17 | try { 18 | TimeUnit.SECONDS.sleep(1); 19 | } catch (InterruptedException ex) { 20 | 21 | } 22 | name = "cmazxiaoma"; 23 | } 24 | 25 | private class MyThread implements Runnable { 26 | @Override 27 | public void run() { 28 | System.out.println(Escape.this.name); 29 | } 30 | } 31 | 32 | public static void main(String[] args) { 33 | new Escape(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/redis/BasePrefix.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.redis; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/5/10 12:35 8 | */ 9 | public abstract class BasePrefix implements KeyPrefix { 10 | 11 | private int expireSeconds; 12 | 13 | private String prefix; 14 | 15 | public BasePrefix(int expireSeconds, String prefix) { 16 | this.expireSeconds = expireSeconds; 17 | this.prefix = prefix; 18 | } 19 | 20 | public BasePrefix(String prefix) { 21 | this(0, prefix); 22 | } 23 | 24 | @Override 25 | public int expireSeconds() { 26 | return expireSeconds; 27 | } 28 | 29 | @Override 30 | public String getPrefix() { 31 | String className = getClass().getSimpleName(); 32 | return className + ":" + prefix; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/service/StudentTestService.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.service; 2 | 3 | import com.cmazxiaoma.hibernate.IStudentDao; 4 | import com.cmazxiaoma.model.Student; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | /** 10 | * @author cmazxiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2018/10/17 14:33 14 | */ 15 | @Service 16 | @Transactional 17 | public class StudentTestService { 18 | 19 | @Autowired 20 | private IStudentDao studentDao; 21 | 22 | public void save(Student student) { 23 | studentDao.save(student); 24 | if (true) { 25 | throw new RuntimeException("回滚"); 26 | } 27 | // Integer pengsai = Integer.parseInt(null); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/controller/handlerMethod/GetController.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.controller.handlerMethod; 2 | 3 | import org.springframework.web.bind.WebDataBinder; 4 | import org.springframework.web.bind.annotation.*; 5 | 6 | /** 7 | * @author xiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2019/4/7 0:22 11 | */ 12 | @RestController 13 | @RequestMapping("/test") 14 | public class GetController { 15 | 16 | @GetMapping("/detail/{id}") 17 | public String detail(@PathVariable(name = "id") String id) { 18 | return id; 19 | } 20 | 21 | @InitBinder("user") 22 | public void initUser(WebDataBinder binder) { 23 | binder.setFieldDefaultPrefix("user."); 24 | 25 | } 26 | 27 | @InitBinder("admin") 28 | public void initAdmin(WebDataBinder binder) { 29 | binder.setFieldDefaultPrefix("admin."); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/lotteryTest/Mall11LotteryResultVo.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.lotteryTest; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Mall11LotteryResultVo implements Serializable { 6 | 7 | private Long goodId; 8 | 9 | private String goodName; 10 | 11 | private String goodImgUrl; 12 | 13 | 14 | public Long getGoodId() { 15 | return goodId; 16 | } 17 | 18 | public void setGoodId(Long goodId) { 19 | this.goodId = goodId; 20 | } 21 | 22 | public String getGoodName() { 23 | return goodName; 24 | } 25 | 26 | public void setGoodName(String goodName) { 27 | this.goodName = goodName; 28 | } 29 | 30 | public String getGoodImgUrl() { 31 | return goodImgUrl; 32 | } 33 | 34 | public void setGoodImgUrl(String goodImgUrl) { 35 | this.goodImgUrl = goodImgUrl; 36 | } 37 | } -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/JiechengTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/28 15:14 8 | */ 9 | public class JiechengTest { 10 | 11 | /** 12 | * 是否是2的阶乘 13 | * @param n 14 | * @return 15 | */ 16 | public static boolean isJieCheng(Long n) { 17 | return (n & n - 1) == 0; 18 | } 19 | 20 | public static void main(String[] args) { 21 | System.out.println(isJieCheng(1L)); 22 | System.out.println(isJieCheng(2L)); 23 | System.out.println(isJieCheng(3L)); 24 | System.out.println(isJieCheng(4L)); 25 | System.out.println(isJieCheng(8L)); 26 | System.out.println(isJieCheng(16L)); 27 | System.out.println(isJieCheng(32L)); 28 | System.out.println(isJieCheng(64L)); 29 | } 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/service/impl/StudentServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.service.impl; 2 | 3 | import com.cmazxiaoma.InitSpringTest; 4 | import com.cmazxiaoma.model.Student; 5 | import com.cmazxiaoma.service.IStudentService; 6 | import org.junit.Test; 7 | 8 | import javax.annotation.Resource; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2018/7/6 15:49 15 | */ 16 | public class StudentServiceImplTest extends InitSpringTest { 17 | 18 | @Resource 19 | private IStudentService studentService; 20 | 21 | @Test 22 | public void findOneByV2() throws Exception { 23 | Student student1 = studentService.findOneByV2("1"); 24 | System.out.println("student1=" + student1); 25 | 26 | Student student2 = studentService.findOneByV2("1"); 27 | System.out.println("student2=" + student2); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/quzhuanxiang/ConsumerDemo2.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.quzhuanxiang; 2 | 3 | import java.util.function.Consumer; 4 | 5 | /** 6 | * @author xiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/10/8 18:59 10 | */ 11 | 12 | public class ConsumerDemo2 { 13 | 14 | public static void printInfo(String[] strArr, Consumer con1, Consumer con2) { 15 | 16 | for (int i = 0; i < strArr.length; i++) { 17 | con1.andThen(con2).accept(strArr[i]); 18 | } 19 | 20 | } 21 | 22 | public static void main(String[] args) { 23 | String[] strArr = {"迪丽热巴,女", "郑爽,女", "杨紫,女"}; 24 | printInfo(strArr, (message) -> { 25 | System.out.print("姓名:" + message.split(",")[0] + "。 "); 26 | }, (message) -> { 27 | System.out.println("性别:" + message.split(",")[1] + "。"); 28 | 29 | }); 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; 7 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 8 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 9 | import org.springframework.transaction.annotation.EnableTransactionManagement; 10 | 11 | @SpringBootApplication 12 | // 启动注解事务管理,等同于xml配置方式的 13 | @EnableTransactionManagement 14 | @EnableCircuitBreaker 15 | @EnableHystrix 16 | @EnableHystrixDashboard 17 | public class DemoApplication { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(DemoApplication.class, args); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/CustomThreadPoolExecutor.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent; 2 | 3 | import java.util.concurrent.*; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/8/16 10:23 10 | */ 11 | public class CustomThreadPoolExecutor extends ThreadPoolExecutor { 12 | 13 | 14 | public CustomThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) { 15 | super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); 16 | } 17 | 18 | public static class CustomTask extends FutureTask { 19 | 20 | public CustomTask(Callable callable) { 21 | super(callable); 22 | } 23 | 24 | public CustomTask(Runnable runnable, V result) { 25 | super(runnable, result); 26 | } 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/inject/MyHibernateDaoSupport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 文件名:MyHibernateDaoSupport.java
3 | * 描述:
4 | * 开发人员:wutao
5 | * 创建时间: 2018-9-26 6 | */ 7 | 8 | package com.cmazxiaoma.spring.inject; 9 | 10 | 11 | /** 12 | * 类名: MyHibernateDaoSupport
13 | * 描述:
14 | * 开发人员: wutao
15 | * 创建时间: 2018-9-26 16 | */ 17 | public class MyHibernateDaoSupport { 18 | 19 | private String template; 20 | 21 | /** 22 | * 描述: 设置 mySessionFactory
23 | * @param mySessionFactory 24 | */ 25 | 26 | public void setMySessionFactory(MySessionFactory mySessionFactory) { 27 | createTemplate(mySessionFactory); 28 | } 29 | 30 | public void createTemplate(MySessionFactory mySessionFactory) { 31 | this.template = mySessionFactory.getName(); 32 | } 33 | 34 | public String getTemplate() { 35 | return this.template; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/ThreadTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent; 2 | 3 | import java.util.concurrent.*; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/9/4 20:45 10 | */ 11 | public class ThreadTest { 12 | 13 | public static void main(String[] args) { 14 | test(); 15 | } 16 | 17 | public static void test() { 18 | System.out.println(Thread.currentThread().getId() + ":" + Thread.currentThread().getName()); 19 | ThreadFactory threadFactory = Executors.defaultThreadFactory(); 20 | Thread thread = threadFactory.newThread(Thread.currentThread()); 21 | System.out.println(thread.getId() + ":" + thread.getName()); 22 | 23 | ThreadPoolExecutor threadPoolExecutor = 24 | new ThreadPoolExecutor(1,1,0L, TimeUnit.SECONDS, 25 | new ArrayBlockingQueue(1)); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/coupon/CouponTextVo.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.coupon; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | import java.util.Date; 7 | 8 | /** 9 | * @author xiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2019/11/8 20:06 13 | */ 14 | @Data 15 | public class CouponTextVo implements Serializable { 16 | 17 | /** 18 | * -- couponId 优惠券ID 19 | -- couponExpireStartTime 优惠券有效期开始时间 20 | -- couponExpireEndTime 优惠券有效期结束时间 21 | -- couponRedirectType 优惠券跳转类型, 0是点击券面直接领取, 1是跳转链接, 2是跳转商详 22 | -- couponRedirectUrl 优惠券跳转路由 23 | -- couponRedirectSourceId 优惠券跳转商详, 商品ID 24 | */ 25 | 26 | private Long couponId; 27 | private Date couponExpireStartTime; 28 | private Date couponExpireEndTime; 29 | private Integer couponRedirectType; 30 | private String couponRedirectUrl; 31 | private String couponRedirectSourceId; 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/service/impl/StudentTestServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.service.impl; 2 | 3 | import com.cmazxiaoma.InitSpringTest; 4 | import com.cmazxiaoma.model.Student; 5 | import com.cmazxiaoma.service.StudentTestService; 6 | import org.junit.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | 9 | import java.util.Date; 10 | 11 | /** 12 | * @author cmazxiaoma 13 | * @version V1.0 14 | * @Description: TODO 15 | * @date 2018/10/17 14:35 16 | */ 17 | public class StudentTestServiceTest extends InitSpringTest { 18 | 19 | @Autowired 20 | private StudentTestService studentTestService; 21 | 22 | @Test 23 | public void testSave() { 24 | Student student = new Student(); 25 | student.setStudentName("pengsai123"); 26 | student.setIsDel("0"); 27 | student.setCreatedDt(new Date()); 28 | student.setUpdatedDt(new Date()); 29 | 30 | studentTestService.save(student); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/proxy/CglibProxy.java: -------------------------------------------------------------------------------- 1 | package com.proxy; 2 | 3 | import org.mockito.cglib.proxy.MethodInterceptor; 4 | import org.mockito.cglib.proxy.MethodProxy; 5 | import org.springframework.aop.support.AopUtils; 6 | 7 | import java.lang.reflect.Method; 8 | import java.text.MessageFormat; 9 | 10 | /** 11 | * @author xiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2020/1/29 17:35 15 | */ 16 | public class CglibProxy implements MethodInterceptor { 17 | 18 | @Override 19 | public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { 20 | System.out.println(obj.getClass().getSuperclass().getSimpleName()); 21 | System.out.println(MessageFormat.format("代理子类:{0},method:{1},args:{2},proxy:{3}", obj.getClass().getSimpleName() 22 | 23 | , method.getDeclaringClass().getSimpleName(), args.toString(), proxy.getSuperName())); 24 | return proxy.invokeSuper(obj, args); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/controller/v2/BaseController1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.controller.v2; 2 | 3 | import org.springframework.web.bind.annotation.ModelAttribute; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | import javax.servlet.http.HttpSession; 8 | 9 | /** 10 | * @author cmazxiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2018/8/7 16:15 14 | */ 15 | public abstract class BaseController1 { 16 | 17 | protected HttpServletRequest request; 18 | 19 | protected HttpServletResponse response; 20 | 21 | protected HttpSession httpSession; 22 | 23 | 24 | @ModelAttribute 25 | public synchronized void init(HttpServletRequest request, 26 | HttpServletResponse response, 27 | HttpSession httpSession) { 28 | this.request = request; 29 | this.response = response; 30 | this.httpSession = httpSession; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/9/27 17:52 10 | */ 11 | public class Test { 12 | 13 | 14 | public static void main(String[] args) { 15 | test2(); 16 | } 17 | 18 | 19 | public static void test1() { 20 | 21 | new Thread(new Runnable() { 22 | @Override 23 | public void run() { 24 | try { 25 | TimeUnit.SECONDS.sleep(5); 26 | } catch (Exception ex) { 27 | ex.printStackTrace(); 28 | } 29 | } 30 | }).start(); 31 | 32 | System.out.println("hello"); 33 | } 34 | 35 | public static void test2() { 36 | try { 37 | TimeUnit.SECONDS.sleep(5); 38 | } catch (Exception ex) { 39 | 40 | } 41 | System.out.println("hello"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/controller/test/ThreadLocalTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.controller.test; 2 | 3 | import org.springframework.util.ReflectionUtils; 4 | 5 | import java.lang.reflect.Field; 6 | import java.lang.reflect.Modifier; 7 | import java.util.concurrent.TimeUnit; 8 | 9 | /** 10 | * @author xiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2019/4/18 13:35 14 | */ 15 | public class ThreadLocalTest { 16 | 17 | public static ThreadLocal threadLocal = new ThreadLocal<>(); 18 | 19 | public static void main(String[] args) throws InterruptedException, IllegalAccessException { 20 | for (int i = 0; i < 5; i++) { 21 | new Thread(new MyThreadLocalThread()).start(); 22 | } 23 | } 24 | 25 | static class MyThreadLocalThread extends Thread { 26 | 27 | @Override 28 | public void run() { 29 | threadLocal.set(this.getName()); 30 | System.out.println(threadLocal.get()); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/blockingqueue/PriorityTask.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.blockingqueue; 2 | 3 | import org.springframework.web.util.HtmlUtils; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/9/27 18:05 10 | */ 11 | public class PriorityTask implements Comparable { 12 | 13 | private Integer num; 14 | 15 | public PriorityTask(Integer num) { 16 | this.num = num; 17 | } 18 | 19 | public Integer getNum() { 20 | return num; 21 | } 22 | 23 | @Override 24 | public int compareTo(PriorityTask item) { 25 | if (this.getNum() > item.getNum()) { 26 | return 1; 27 | } else if (this.getNum() < item.getNum()) { 28 | return -1; 29 | } 30 | return 0; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | 36 | return "PriorityTask{" + 37 | "num=" + num + 38 | '}'; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/java/Test5.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.java; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/16 23:32 8 | */ 9 | public class Test5 { 10 | public static Test5 test5 = new Test5(); 11 | 12 | static { 13 | System.out.println(1); 14 | } 15 | 16 | static { 17 | System.out.println(2); 18 | } 19 | 20 | { 21 | System.out.println(3); 22 | } 23 | 24 | public Test5() { 25 | System.out.println(4); 26 | } 27 | 28 | { 29 | System.out.println(5); 30 | } 31 | 32 | static { 33 | System.out.println(7); 34 | } 35 | 36 | public static void main(String[] args) { 37 | System.out.println(6); 38 | new Test5(); 39 | 40 | /** 41 | * 3 42 | * 5 43 | * 4 44 | * 1 45 | * 2 46 | * 7 47 | * 6 48 | * 3 49 | * 5 50 | * 4 51 | */ 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/controller/ThreadController.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.controller; 2 | 3 | import com.cmazxiaoma.hibernate.ISchoolDao; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2018/11/9 18:00 15 | */ 16 | @RestController 17 | @RequestMapping("/thread") 18 | public class ThreadController { 19 | 20 | @Autowired 21 | private ISchoolDao schoolDao; 22 | 23 | @GetMapping("/test") 24 | public void test() { 25 | System.out.println("thread test"); 26 | new Thread(new Runnable() { 27 | @Override 28 | public void run() { 29 | schoolDao.findAll(); 30 | } 31 | }).start(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/linkedList/Node.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.linkedList; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/7/20 9:10 8 | */ 9 | public class Node { 10 | private Node next; 11 | private final int value; 12 | 13 | public Node getNext() { 14 | return next; 15 | } 16 | 17 | public void setNext(Node next) { 18 | this.next = next; 19 | } 20 | 21 | public int getValue() { 22 | return value; 23 | } 24 | 25 | public Node(int value) { 26 | super(); 27 | this.value = value; 28 | this.next = null; 29 | } 30 | 31 | public static void prinfLinkedList(Node head) { 32 | 33 | while (head != null) { 34 | System.out.print(head.getValue() + " "); 35 | head = head.getNext(); 36 | } 37 | System.out.println(""); 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return "Node [next=" + next + ", value=" + value + "]"; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/quzhuanxiang/Test2.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.quzhuanxiang; 2 | 3 | import org.apache.commons.lang.time.DateFormatUtils; 4 | 5 | import java.util.Date; 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | /** 9 | * @author xiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2019/9/24 10:33 13 | */ 14 | public class Test2 { 15 | 16 | public static void main(String[] args) { 17 | for (int i = 0; i < 1000; i++) { 18 | System.out.println(ThreadLocalRandom.current().nextInt(2)); 19 | } 20 | 21 | Date endTime = DateUtils.addHours(DateUtils.getLastOfDate(new Date()), -1); 22 | System.out.println(DateFormatUtils.format(endTime, "yyyy-MM-dd HH:mm:ss")); 23 | 24 | Date zeroDay = DateUtils.getDayBefore(new Date(), 0); 25 | System.out.println(DateFormatUtils.format(zeroDay, "yyyy-MM-dd HH:mm:ss")); 26 | 27 | int[] a = new int[] {1,2,3,4,5}; 28 | 29 | for (int b: a) { 30 | System.out.println("b:" + b); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/retry/HttpController.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.retry; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.util.concurrent.atomic.LongAdder; 8 | 9 | /** 10 | * @author xiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2019/6/12 12:26 14 | */ 15 | @RestController 16 | @RequestMapping("/http") 17 | public class HttpController { 18 | 19 | private LongAdder longAdder = new LongAdder(); 20 | 21 | @GetMapping("/demo") 22 | public String demo() { 23 | longAdder.increment(); 24 | 25 | if (longAdder.intValue() % 4 == 0) { 26 | System.out.println(longAdder.intValue() + ":success"); 27 | return "success"; 28 | } else { 29 | System.out.println(longAdder.intValue() + ":fail"); 30 | // throw new RuntimeException("fail"); 31 | return "fail"; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/Javaoffer/classLoader/ClassLoaderChecker.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.Javaoffer.classLoader; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2019/2/18 22:19 10 | */ 11 | public class ClassLoaderChecker { 12 | 13 | public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, UnsupportedEncodingException { 14 | MyClassLoader myClassLoader = new MyClassLoader("D:\\dev\\intellij-workspace\\hibernate_demo\\src\\main\\java\\com\\cmazxiaoma\\Javaoffer\\classLoader\\", "MyClassLoader"); 15 | Class myClass = myClassLoader.loadClass("Data"); 16 | 17 | System.out.println(myClassLoader); 18 | System.out.println(myClassLoader.getParent()); 19 | System.out.println(myClassLoader.getParent().getParent()); 20 | System.out.println(myClassLoader.getParent().getParent().getParent()); 21 | 22 | Object object = myClass.newInstance(); 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/controller/test/Parent.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.controller.test; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/4/17 23:19 8 | */ 9 | public class Parent { 10 | 11 | public String publicField = "1"; 12 | 13 | String defaultField = "2"; 14 | 15 | protected String protectedField = "3"; 16 | 17 | private String privateField = "4" ; 18 | 19 | public void publicMethod() { 20 | System.out.println("publicMethod..."); 21 | } 22 | 23 | void defaultMethod() { 24 | System.out.println("defaultMethod..."); 25 | } 26 | 27 | protected void protectedMethod() { 28 | System.out.println("protectedMethod..."); 29 | } 30 | 31 | private void privateMethod() { 32 | System.out.println("privateMethod..."); 33 | } 34 | 35 | static class LocalMap { 36 | 37 | private static String table = "table"; 38 | 39 | static class Entry { 40 | private static String name = "entry"; 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/UserBatchCommand.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | import com.netflix.hystrix.HystrixCommand; 4 | import com.netflix.hystrix.HystrixCommandGroupKey; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @author xiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2019/5/31 11:01 13 | */ 14 | public class UserBatchCommand extends HystrixCommand> { 15 | 16 | private UserService userService; 17 | 18 | private List userIds; 19 | 20 | public UserBatchCommand(UserService userService, 21 | List userIds) { 22 | super(Setter.withGroupKey(HystrixCommandGroupKey.Factory 23 | .asKey("userBatchCommand"))); 24 | this.userIds = userIds; 25 | this.userService = userService; 26 | } 27 | 28 | @Override 29 | protected List run() throws Exception { 30 | System.out.println("=============Hystrix批量处理请求。。。================"); 31 | return userService.findAll(userIds); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/JUC/ThreadInterruptTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.JUC; 2 | 3 | import com.cmazxiaoma.quzhuanxiang.DateUtils; 4 | 5 | import java.util.Date; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2019/2/16 14:56 12 | */ 13 | public class ThreadInterruptTest { 14 | 15 | public static void main(String[] args) { 16 | Thread thread = new Thread(new Runnable() { 17 | @Override 18 | public void run() { 19 | for (;;) { 20 | if (Thread.interrupted()) { 21 | break; 22 | } 23 | System.out.println("i want interrupted"); 24 | } 25 | 26 | 27 | } 28 | }); 29 | 30 | thread.start(); 31 | 32 | thread.interrupt(); 33 | 34 | Date startDate = DateUtils.getDayBefore(new Date(), 7); 35 | Date endDate = DateUtils.getDayBefore(new Date(), 1); 36 | 37 | System.out.println(startDate + "," + endDate); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/MyThreadLocal.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent; 2 | 3 | import java.text.DateFormat; 4 | import java.text.ParseException; 5 | import java.text.SimpleDateFormat; 6 | import java.util.Date; 7 | 8 | /** 9 | * @author cmazxiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2018/11/20 12:43 13 | */ 14 | public class MyThreadLocal { 15 | 16 | public static final ThreadLocal DATE_FORMAT_THREAD_LOCAL = new ThreadLocal(){ 17 | @Override 18 | protected DateFormat initialValue() { 19 | return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 20 | } 21 | }; 22 | 23 | public static String format(Date date) { 24 | return DATE_FORMAT_THREAD_LOCAL.get().format(date); 25 | } 26 | 27 | public static Date parse(String date) throws ParseException { 28 | return DATE_FORMAT_THREAD_LOCAL.get().parse(date); 29 | 30 | } 31 | 32 | public static void main(String[] args) { 33 | String date = format(new Date()); 34 | System.out.println(date); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/lock/MyReentrantLock.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.lock; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/8/17 17:12 8 | */ 9 | public class MyReentrantLock implements ILock { 10 | 11 | private Thread lockedByThread = null; 12 | 13 | private int count; 14 | 15 | private boolean isLocked = false; 16 | 17 | @Override 18 | public synchronized void lock() throws InterruptedException { 19 | Thread thread = Thread.currentThread(); 20 | 21 | while (isLocked && thread != lockedByThread) { 22 | wait(); 23 | } 24 | 25 | isLocked = true; 26 | count++; 27 | lockedByThread = thread; 28 | } 29 | 30 | @Override 31 | public synchronized void unLock() { 32 | Thread thread = Thread.currentThread(); 33 | 34 | if (thread == lockedByThread) { 35 | count --; 36 | if (count == 0) { 37 | isLocked = false; 38 | notify(); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/twozerotwozero/RedLock.java: -------------------------------------------------------------------------------- 1 | package com.twozerotwozero; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2020/3/2 21:45 8 | */ 9 | public class RedLock { 10 | 11 | private String $1; 12 | 13 | /** 14 | * 在分布式版本的算法里我们假设我们有N个Redis master节点,这些节点都是完全独立的, 15 | * 我们不用任何复制或者其他隐含的分布式协调算法。 16 | * 我们已经描述了如何在单节点环境下安全地获取和释放锁。 17 | * 因此我们理所当然地应当用这个方法在每个单节点里来获取和释放锁。在我们的例子里面我们把N设成5, 18 | * 这个数字是一个相对比较合理的数值,因此我们需要在不同的计算机或者虚拟机上运行5个master节点来保证他们大多数情况下都不会同时宕机。一个客户端需要做如下操作来获取锁: 19 | 20 | 1.获取当前时间(单位是毫秒)。轮流用相同的key和随机值在N个节点上请求锁, 21 | 在这一步里,客户端在每个master上请求锁时,会有一个和总的锁释放时间相比小的多的超时时间。 22 | 比如如果锁自动释放时间是10秒钟,那每个节点锁请求的超时时间可能是5-50毫秒的范围, 23 | 这个可以防止一个客户端在某个宕掉的master节点上阻塞过长时间, 24 | 如果一个master节点不可用了,我们应该尽快尝试下一个master节点。 25 | 2.客户端计算第二步中获取锁所花的时间,只有当客户端在大多数master节点上成功获取了锁(在这里是3个), 26 | 而且总共消耗的时间不超过锁释放时间,这个锁就认为是获取成功了。 27 | 3.如果锁获取成功了,那现在锁自动释放时间就是最初的锁释放时间减去之前获取锁所消耗的时间。 28 | 4.如果锁获取失败了,不管是因为获取成功的锁不超过一半(N/2+1)还是因为总消耗时间超过了锁释放时间, 29 | 客户端都会到每个master节点上释放锁,即便是那些他认为没有获取成功的锁。 30 | */ 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cvte/InsertSort.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.cvte; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/4/15 21:53 8 | */ 9 | public class InsertSort { 10 | 11 | private static int[] array = {4,2,1,4,67,656}; 12 | 13 | public static void display() { 14 | for (int i = 0; i < array.length; i++) { 15 | System.out.println(array[i]); 16 | } 17 | } 18 | 19 | public static void swap(int a, int b) { 20 | int temp = array[b]; 21 | array[b] = array[a]; 22 | array[a] = temp; 23 | } 24 | 25 | public static void sort0() { 26 | for (int i = 0; i < array.length; i++) { 27 | int value = array[i]; 28 | int j = i - 1; 29 | 30 | while (j >= 0 && value <= array[j]) { 31 | 32 | array[j + 1] = array[j]; 33 | j --; 34 | } 35 | 36 | array[j + 1] = value; 37 | } 38 | } 39 | 40 | public static void main(String[] args) { 41 | sort0(); 42 | display(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/hibernate/MyCommonDaoTest.java: -------------------------------------------------------------------------------- 1 | //package com.cmazxiaoma.hibernate; 2 | // 3 | //import com.cmazxiaoma.InitSpringTest; 4 | //import com.cmazxiaoma.MyCommonDao; 5 | //import org.hibernate.SessionFactory; 6 | //import org.junit.Test; 7 | //import org.springframework.beans.factory.annotation.Autowired; 8 | //import org.springframework.context.ApplicationContext; 9 | // 10 | ///** 11 | // * @author cmazxiaoma 12 | // * @version V1.0 13 | // * @Description: TODO 14 | // * @date 2018/9/20 17:59 15 | // */ 16 | //public class MyCommonDaoTest extends InitSpringTest { 17 | // 18 | // @Autowired 19 | // private SessionFactory sessionFactory; 20 | // 21 | // @Autowired 22 | // private ApplicationContext applicationContext; 23 | // 24 | // @Autowired 25 | // private MyCommonDao myCommonDao; 26 | // 27 | // @Test 28 | // public void test() { 29 | // SessionFactory sessionFactory = (SessionFactory) 30 | // applicationContext.getBean("sessionFactory"); 31 | // System.out.println(sessionFactory); 32 | // System.out.println(myCommonDao.list()); 33 | // } 34 | //} 35 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/SpringApplicationContext.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring; 2 | 3 | import lombok.extern.apachecommons.CommonsLog; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.ApplicationContextAware; 6 | import org.springframework.context.annotation.Conditional; 7 | import org.springframework.stereotype.Component; 8 | 9 | /** 10 | * @author cmazxiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2018/8/10 14:47 14 | */ 15 | @Component 16 | public class SpringApplicationContext implements ApplicationContextAware { 17 | 18 | public static ApplicationContext applicationContext; 19 | 20 | public static ApplicationContext getApplicationContext() { 21 | return applicationContext; 22 | } 23 | 24 | @Override 25 | public void setApplicationContext(ApplicationContext applicationContext) { 26 | SpringApplicationContext.applicationContext = applicationContext; 27 | } 28 | 29 | public static T getBeanByType(Class requiredType) { 30 | return applicationContext.getBean(requiredType); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/date/LocalTimeTest1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.date; 2 | 3 | import com.cmazxiaoma.quzhuanxiang.DateUtils; 4 | 5 | import java.sql.Timestamp; 6 | import java.time.LocalDateTime; 7 | import java.time.ZoneId; 8 | import java.util.Date; 9 | 10 | /** 11 | * @author xiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2020/3/8 13:41 15 | */ 16 | public class LocalTimeTest1 { 17 | 18 | public static void main(String[] args) throws Exception { 19 | Date currentDate = DateUtils.parseDate("2017-07-01 06:00:00", "yyyy-MM-dd HH:mm:ss"); 20 | 21 | long current = currentDate.getTime(); 22 | 23 | System.out.println("current:" + current); 24 | System.out.println("epochMilli:" + currentDate.toInstant().toEpochMilli()); 25 | 26 | System.out.println("date:" + LocalDateTime.ofInstant( 27 | currentDate.toInstant(), 28 | ZoneId.systemDefault() 29 | )); 30 | 31 | Date newCurrentDate = new Date(current); 32 | System.out.println(newCurrentDate); 33 | System.out.println(new Timestamp(current)); 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/netty/demo/oio/PlainOioClient.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.netty.demo.oio; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.net.InetSocketAddress; 7 | import java.net.Socket; 8 | 9 | /** 10 | * @author cmazxiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2018/12/6 17:37 14 | */ 15 | public class PlainOioClient { 16 | 17 | public static void main(String[] args) throws IOException { 18 | PlainOioClient plainOioClient = new PlainOioClient(); 19 | plainOioClient.connect(); 20 | } 21 | 22 | public void connect() throws IOException { 23 | Socket socket = new Socket(); 24 | socket.connect(new InetSocketAddress("127.0.0.1", 9998), 5000); 25 | 26 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); 27 | String line = null; 28 | while ((line = bufferedReader.readLine()) != null) { 29 | System.out.println(line); 30 | } 31 | bufferedReader.close(); 32 | socket.close(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/MyCommonDao.java: -------------------------------------------------------------------------------- 1 | //package com.cmazxiaoma; 2 | // 3 | //import org.hibernate.Hibernate; 4 | //import org.hibernate.SessionFactory; 5 | //import org.springframework.beans.factory.annotation.Autowired; 6 | //import org.springframework.context.annotation.Lazy; 7 | //import org.springframework.core.annotation.Order; 8 | //import org.springframework.orm.hibernate4.support.HibernateDaoSupport; 9 | //import org.springframework.stereotype.Component; 10 | //import org.springframework.stereotype.Repository; 11 | //import org.springframework.transaction.annotation.Transactional; 12 | // 13 | ///** 14 | // * @author cmazxiaoma 15 | // * @version V1.0 16 | // * @Description: TODO 17 | // * @date 2018/9/20 17:58 18 | // */ 19 | //@Component 20 | //@Transactional 21 | //public class MyCommonDao extends HibernateDaoSupport { 22 | // 23 | //// @Autowired 24 | //// public void setSessionFactoryOverride(SessionFactory sessionFactory) { 25 | //// super.setSessionFactory(sessionFactory); 26 | //// } 27 | // 28 | // public String list() { 29 | // this.getSessionFactory().getCurrentSession(); 30 | // return "success"; 31 | // } 32 | //} 33 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/file/FileTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.file; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.FileNotFoundException; 5 | import java.io.FileOutputStream; 6 | import java.io.IOException; 7 | 8 | /** 9 | * @author cmazxiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2018/12/24 17:37 13 | */ 14 | public class FileTest { 15 | 16 | public static void main(String[] args) throws IOException { 17 | String readPath = "C:\\Users\\cmazxiaoma\\Desktop\\read.txt"; 18 | String writePath = "C:\\Users\\cmazxiaoma\\Desktop\\write.txt"; 19 | 20 | FileInputStream fileInputStream = new FileInputStream(readPath); 21 | FileOutputStream fileOutputStream = new FileOutputStream(writePath); 22 | 23 | int hasRead = 0; 24 | byte[] bytes = new byte[2]; 25 | 26 | while ((hasRead = fileInputStream.read(bytes)) != -1) { 27 | System.out.println("hasRead=" + hasRead); 28 | System.out.println("bytes=" + bytes); 29 | 30 | // offset,len 分别指的是bytes的偏移量、bytes的大小。 31 | fileOutputStream.write(bytes, 0, hasRead); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/Test4.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2020/1/2 12:17 8 | */ 9 | public class Test4 { 10 | 11 | public static void main(String[] args) { 12 | User user = new User(); 13 | user.setId("1"); 14 | System.out.println(user); 15 | user(user); 16 | System.out.println(user); 17 | 18 | double sd1 = 0.00D; 19 | System.out.println(sd1 > 0.00D); 20 | } 21 | 22 | public static User user(User user) { 23 | User newUser = new User(); 24 | newUser.setId("2"); 25 | user = newUser; 26 | return null; 27 | } 28 | 29 | public static class User { 30 | private String id; 31 | 32 | public String getId() { 33 | return id; 34 | } 35 | 36 | public void setId(String id) { 37 | this.id = id; 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return "User{" + 43 | "id='" + id + '\'' + 44 | '}'; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/netty/demo/aio/SimpleAioClient.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.netty.demo.aio; 2 | 3 | import java.io.IOException; 4 | import java.net.InetSocketAddress; 5 | import java.nio.ByteBuffer; 6 | import java.nio.channels.AsynchronousSocketChannel; 7 | import java.nio.charset.Charset; 8 | import java.util.concurrent.ExecutionException; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2019/2/27 22:39 15 | */ 16 | public class SimpleAioClient { 17 | 18 | static final int port = 8887; 19 | 20 | public static void main(String[] args) throws IOException, ExecutionException, InterruptedException { 21 | ByteBuffer buffer = ByteBuffer.allocate(1024); 22 | Charset charset = Charset.forName("UTF-8"); 23 | 24 | AsynchronousSocketChannel clientChannel = AsynchronousSocketChannel.open(); 25 | clientChannel.connect(new InetSocketAddress("127.0.0.1", port)).get(); 26 | 27 | clientChannel.read(buffer).get(); 28 | buffer.flip(); 29 | 30 | String content = charset.decode(buffer).toString(); 31 | System.out.println("服务器消息:" + content); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/JUC/UnsafeParkTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.JUC; 2 | 3 | import org.springframework.util.ReflectionUtils; 4 | import sun.misc.Unsafe; 5 | 6 | import java.lang.reflect.Constructor; 7 | import java.util.concurrent.locks.LockSupport; 8 | 9 | /** 10 | * @author cmazxiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2019/2/15 22:34 14 | */ 15 | public class UnsafeParkTest { 16 | 17 | public static Unsafe unsafe; 18 | 19 | static { 20 | try { 21 | Constructor constructor = Unsafe.class.getDeclaredConstructor(); 22 | ReflectionUtils.makeAccessible(constructor); 23 | unsafe = constructor.newInstance(); 24 | } catch (Exception e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | 29 | public static void main(String[] args) { 30 | Thread thread = new Thread(new Runnable() { 31 | @Override 32 | public void run() { 33 | // 一直阻塞 34 | unsafe.park(false, 0L); 35 | } 36 | }); 37 | thread.start(); 38 | thread.interrupt(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/chengyu/Test.java: -------------------------------------------------------------------------------- 1 | package com.chengyu; 2 | 3 | import com.cmazxiaoma.quzhuanxiang.DateUtils; 4 | import com.google.common.collect.Lists; 5 | import org.apache.commons.lang.time.DateFormatUtils; 6 | import org.springframework.util.CollectionUtils; 7 | 8 | import java.util.Collections; 9 | import java.util.Date; 10 | import java.util.List; 11 | 12 | /** 13 | * @author xiaoma 14 | * @version V1.0 15 | * @Description: TODO 16 | * @date 2020/1/14 15:07 17 | */ 18 | public class Test { 19 | 20 | public static void main(String[] args) { 21 | // 1个小时 3个体力 10个小时 30个体力 100个小时 300个体力 22 | System.out.println(DateUtils.addHours(new Date(), -100).getTime()); 23 | 24 | // {"data":[{"key":"key_tilitime","value":"1578982820745"}],"session_id":"u11XckV6RQRwM531v6Jqd"} 25 | 26 | System.out.println(DateFormatUtils.format(new Date(1578986420745L), "yyyy-MM-dd HH:mm:ss")); 27 | 28 | System.out.println(DateFormatUtils.format(new Date(1578982820745L), "yyyy-MM-dd HH:mm:ss")); 29 | 30 | 31 | List list = Lists.newArrayList(7,2,1); 32 | Collections.sort(list); 33 | System.out.println(list); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/贪心算法/TestTanxin.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba.贪心算法; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: 贪心算法 找硬币问题 7 | * @date 2020/2/14 22:18 8 | */ 9 | 10 | /** 11 | * 当n>=5时, 我们尽可能多剪长度为3的绳子 12 | * 当剩下的绳子长度为4 把绳子剪成两段 13 | * 长度为2的绳子 14 | * 当n>=5的时候, 2(n-2)>n, 3(n-3)>n, 3(n-3) >2(n-2) 15 | * 当n=4的时候,2*2>1*3 2(n-2)>3(n-3) 16 | */ 17 | public class TestTanxin { 18 | 19 | public static void main(String[] args) { 20 | System.out.println(cal(5)); 21 | System.out.println(cal(6)); 22 | System.out.println(cal(7)); 23 | System.out.println(cal(8)); 24 | } 25 | 26 | public static int cal(int length) { 27 | if (length < 2) { 28 | return 0; 29 | } 30 | if (length == 2) { 31 | return 1; 32 | } 33 | if (length == 3) { 34 | return 2; 35 | } 36 | 37 | int var3 = length / 3; 38 | 39 | if (length - 3 * var3 == 1) { 40 | var3 -= 1; 41 | } 42 | int var2 = (length - 3 * var3) / 2; 43 | return (int) (Math.pow(3, var3) * Math.pow(2, var2)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/Hystrix/HystrixConfig.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.Hystrix; 2 | 3 | import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect; 4 | import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; 5 | import org.springframework.boot.web.servlet.ServletRegistrationBean; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | /** 10 | * @author cmazxiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2019/2/17 21:57 14 | */ 15 | @Configuration 16 | public class HystrixConfig { 17 | 18 | @Bean 19 | public HystrixCommandAspect hystrixCommandAspect() { 20 | return new HystrixCommandAspect(); 21 | } 22 | 23 | /** 24 | * 向监控中心Dashboard发送stream消息 25 | */ 26 | @Bean 27 | public ServletRegistrationBean hystrixMetricsStreamServlet() { 28 | ServletRegistrationBean registrationBean = 29 | new ServletRegistrationBean(new HystrixMetricsStreamServlet()); 30 | registrationBean.addUrlMappings("/hystrix.stream"); 31 | return registrationBean; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/compoent/SpringTest4.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.compoent; 2 | 3 | import com.cmazxiaoma.spring.CommonSpringTest; 4 | import com.cmazxiaoma.spring.bean.SpringTest1; 5 | import com.cmazxiaoma.spring.bean.SpringTest2; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2018/8/13 9:24 15 | */ 16 | @Component 17 | public class SpringTest4 { 18 | 19 | @Value("${springTest4.name}") 20 | private String name; 21 | 22 | @Autowired 23 | private SpringTest1 springTest1; 24 | 25 | @Autowired 26 | private SpringTest2 springTest2; 27 | 28 | @Override 29 | public String toString() { 30 | return "SpringTest4{" + 31 | "name='" + name + '\'' + 32 | ", springTest1=" + springTest1 + 33 | ", springTest2=" + springTest2 + 34 | '}'; 35 | } 36 | 37 | public static void main(String[] args) { 38 | System.out.println(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/elasticsearch/Book.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.elasticsearch; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import org.apache.ibatis.annotations.Update; 7 | import org.hibernate.exception.DataException; 8 | import org.hibernate.validator.constraints.NotBlank; 9 | import org.springframework.format.annotation.DateTimeFormat; 10 | 11 | import javax.validation.constraints.NotNull; 12 | import java.util.Date; 13 | 14 | /** 15 | * @author cmazxiaoma 16 | * @version V1.0 17 | * @Description: TODO 18 | * @date 2018/10/24 16:09 19 | */ 20 | @Setter 21 | @Getter 22 | public class Book { 23 | 24 | @NotBlank(message = "id required", groups = Updated.class) 25 | public String id; 26 | 27 | @NotBlank(message = "title required", groups = Added.class) 28 | public String title; 29 | 30 | @NotBlank(message = "author required", groups = Added.class) 31 | public String author; 32 | 33 | @NotBlank(message = "wordCount required", groups = Added.class) 34 | public String wordCount; 35 | 36 | @NotNull(message = "publishDate required", groups = Added.class) 37 | public Date publishDate; 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cvte/SelectSort.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.cvte; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/4/15 21:43 8 | */ 9 | public class SelectSort { 10 | 11 | private static int[] array = {4,2,1,4,67,656}; 12 | 13 | public static void display() { 14 | for (int i = 0; i < array.length; i++) { 15 | System.out.println(array[i]); 16 | } 17 | } 18 | 19 | public static void sort() { 20 | for (int i = 0; i < array.length - 1; i++) { 21 | int minIndex = i; 22 | int min = array[minIndex]; 23 | 24 | for (int j = i; j < array.length; j++) { 25 | if (array[j] < min) { 26 | minIndex = j; 27 | } 28 | } 29 | 30 | if (minIndex != i) { 31 | swap(minIndex, i); 32 | } 33 | } 34 | } 35 | 36 | public static void swap(int a, int b) { 37 | int temp = array[b]; 38 | array[b] = array[a]; 39 | array[a] = temp; 40 | } 41 | 42 | public static void main(String[] args) { 43 | sort(); 44 | display(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/netty/demo/aio/SimpleAioServer.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.netty.demo.aio; 2 | 3 | import java.io.IOException; 4 | import java.net.InetSocketAddress; 5 | import java.nio.ByteBuffer; 6 | import java.nio.channels.AsynchronousServerSocketChannel; 7 | import java.nio.channels.AsynchronousSocketChannel; 8 | import java.util.concurrent.ExecutionException; 9 | import java.util.concurrent.Future; 10 | 11 | /** 12 | * @author cmazxiaoma 13 | * @version V1.0 14 | * @Description: TODO 15 | * @date 2019/2/27 22:38 16 | */ 17 | public class SimpleAioServer { 18 | 19 | static final int port = 8887; 20 | 21 | public static void main(String[] args) throws IOException, ExecutionException, InterruptedException { 22 | AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open(); 23 | serverSocketChannel.bind(new InetSocketAddress(port)); 24 | 25 | while (true) { 26 | Future future = serverSocketChannel.accept(); 27 | AsynchronousSocketChannel socketChannel = future.get(); 28 | socketChannel.write(ByteBuffer.wrap("欢迎来到AIO的世界".getBytes("UTF-8"))).get(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/inject/MyFactoryBean.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.inject; 2 | 3 | import org.springframework.beans.factory.FactoryBean; 4 | import org.springframework.beans.factory.InitializingBean; 5 | import org.springframework.context.annotation.Lazy; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author xiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2019/3/29 23:01 13 | */ 14 | @Component 15 | public class MyFactoryBean implements FactoryBean, InitializingBean { 16 | 17 | private User user; 18 | 19 | @Override 20 | public void afterPropertiesSet() throws Exception { 21 | this.user = buildUser(); 22 | } 23 | 24 | private User buildUser() { 25 | return new User(1L, "cmazxiaoma"); 26 | } 27 | 28 | @Override 29 | public User getObject() throws Exception { 30 | return this.user; 31 | } 32 | 33 | @Override 34 | public Class getObjectType() { 35 | return String.class; 36 | } 37 | 38 | @Override 39 | public boolean isSingleton() { 40 | return true; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "MyFactoryBean{}"; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/date/ZoneTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.date; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | import java.util.TimeZone; 7 | 8 | /** 9 | * @author xiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2020/2/5 17:40 13 | */ 14 | public class ZoneTest { 15 | 16 | public static void main(String[] args) throws ParseException { 17 | String timeStr = "2017-08-24 11:17:10"; // 字面时间 18 | SimpleDateFormat bjSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 19 | bjSdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); 20 | // 1503544630000 21 | Date bjDate = bjSdf.parse(timeStr); // 解析 22 | System.out.println("字面时间: " + timeStr + ",按北京时间来解释:" + bjSdf.format(bjDate) + ", " + bjDate.getTime()); 23 | 24 | SimpleDateFormat tokyoSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 东京 25 | tokyoSdf.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo")); // 设置东京时区 26 | // 1503541030000 27 | Date tokyoDate = tokyoSdf.parse(timeStr); // 解析 28 | System.out.println("字面时间: " + timeStr + ",按东京时间来解释:" + tokyoSdf.format(tokyoDate) + ", " + tokyoDate.getTime()); 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/blockingqueue/ArrayBlockingQueueTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.blockingqueue; 2 | 3 | import java.util.concurrent.ArrayBlockingQueue; 4 | import java.util.concurrent.BlockingQueue; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2018/10/23 9:18 12 | */ 13 | public class ArrayBlockingQueueTest { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | long start = System.currentTimeMillis(); 17 | BlockingQueue blockingQueue = new ArrayBlockingQueue(1); 18 | 19 | blockingQueue.offer("1", 10L, TimeUnit.SECONDS); 20 | String s = blockingQueue.poll(10L, TimeUnit.SECONDS); 21 | System.out.println("s=" + s); 22 | System.out.println(blockingQueue.toString()); 23 | 24 | blockingQueue.offer("2", 10L, TimeUnit.SECONDS); 25 | System.out.println(blockingQueue.toString()); 26 | long end = System.currentTimeMillis(); 27 | 28 | System.out.println((end - start) + "ms"); 29 | String s1 = blockingQueue.poll(10L, TimeUnit.SECONDS); 30 | 31 | System.out.println(blockingQueue.size()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/netty/demo/EchoClinentHandler.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.netty.demo; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | import io.netty.buffer.Unpooled; 5 | import io.netty.channel.ChannelHandler; 6 | import io.netty.channel.ChannelHandlerContext; 7 | import io.netty.channel.SimpleChannelInboundHandler; 8 | import io.netty.util.CharsetUtil; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2018/12/6 14:49 15 | */ 16 | @ChannelHandler.Sharable 17 | public class EchoClinentHandler extends SimpleChannelInboundHandler { 18 | 19 | @Override 20 | public void channelActive(ChannelHandlerContext ctx) throws Exception { 21 | // 当被通知Channel是活跃的话,发送一条消息 22 | ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8)); 23 | } 24 | 25 | @Override 26 | protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { 27 | // 记录消息 28 | System.out.println("Client received :" + msg.toString(CharsetUtil.UTF_8)); 29 | } 30 | 31 | @Override 32 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 33 | cause.printStackTrace(); 34 | ctx.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/mybatis/ISchoolMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.mybatis; 2 | 3 | import com.cmazxiaoma.InitSpringTest; 4 | import com.cmazxiaoma.model.School; 5 | import com.cmazxiaoma.model.Student; 6 | import org.junit.Test; 7 | 8 | import javax.annotation.Resource; 9 | 10 | import java.util.List; 11 | import java.util.Set; 12 | 13 | /** 14 | * @author cmazxiaoma 15 | * @version V1.0 16 | * @Description: TODO 17 | * @date 2018/7/5 17:14 18 | */ 19 | public class ISchoolMapperTest extends InitSpringTest { 20 | 21 | @Resource 22 | private ISchoolMapper schoolMapper; 23 | 24 | @Test 25 | public void findOneByV1() throws Exception { 26 | List schoolList = schoolMapper.findOneByV1("1"); 27 | System.out.println("schoolList=" + schoolList); 28 | System.out.println("school=" + schoolList.get(0)); 29 | System.out.println("studentSet=" + schoolList.get(0).getStudentList()); 30 | } 31 | 32 | @Test 33 | public void findOneByV2() throws Exception { 34 | School school = schoolMapper.findOneByV2("1"); 35 | System.out.println("school=" + school); 36 | Set studentSet = school.getStudentList(); 37 | System.out.println("studentSet=" + studentSet); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/service/impl/StudentServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.service.impl; 2 | 3 | import com.cmazxiaoma.model.Student; 4 | import com.cmazxiaoma.mybatis.IStudentMapper; 5 | import com.cmazxiaoma.service.IStudentService; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import javax.annotation.Resource; 10 | 11 | /** 12 | * @author cmazxiaoma 13 | * @version V1.0 14 | * @Description: TODO 15 | * @date 2018/7/6 15:46 16 | */ 17 | @Service 18 | @Transactional 19 | public class StudentServiceImpl implements IStudentService { 20 | 21 | @Resource 22 | private IStudentMapper studentMapper; 23 | 24 | @Override 25 | public Student findOneByV1(String studentId) { 26 | return studentMapper.findOneByV1(studentId); 27 | } 28 | 29 | @Override 30 | public Student findOneByV2(String studentId) { 31 | return studentMapper.findOneByV2(studentId); 32 | } 33 | 34 | @Override 35 | public Student findOneByV3(String studentId) { 36 | return studentMapper.findOneByV3(studentId); 37 | } 38 | 39 | @Override 40 | public int updateIsDel(String studentId) { 41 | return studentMapper.updateIsDel(studentId); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cvte/BinarySort.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.cvte; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/4/15 21:07 8 | */ 9 | public class BinarySort { 10 | 11 | private static int[] array = {4,2,1,4,67,656}; 12 | 13 | public static void sort() { 14 | for (int i = 0; i < array.length; i++) { 15 | int searchValue = array[i]; 16 | int start = 0; 17 | int end = i - 1; 18 | 19 | while (start <= end) { 20 | int mid = (start + end) / 2; 21 | int value = array[mid]; 22 | 23 | if (searchValue > value) { 24 | start = mid + 1; 25 | } else { 26 | end = mid - 1; 27 | } 28 | } 29 | 30 | for (int k = i; k > start; k--) { 31 | array[k] = array[k - 1]; 32 | } 33 | 34 | array[start] = searchValue; 35 | } 36 | } 37 | 38 | public static void main(String[] args) { 39 | sort(); 40 | display(); 41 | } 42 | 43 | public static void display() { 44 | for (int i = 0; i < array.length; i++) { 45 | System.out.println(array[i]); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/Test3.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Objects; 5 | 6 | /** 7 | * @author xiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2019/11/7 17:48 11 | */ 12 | public class Test3 { 13 | 14 | public static void main(String[] args) { 15 | double str = 0.10; 16 | System.out.println(str > 0.00D); 17 | 18 | 19 | double addMoneyCount = 10000 * 0.23D; 20 | System.out.println(new BigDecimal(addMoneyCount).intValue()); 21 | 22 | System.out.println(new BigDecimal(100000).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue()); 23 | 24 | System.out.println(0.00D/10000); 25 | 26 | BigDecimal rate = new BigDecimal(1).divide(new BigDecimal(3), 2, BigDecimal.ROUND_HALF_UP); 27 | System.out.println(rate); 28 | 29 | System.out.println(System.nanoTime()); 30 | 31 | long key = 0; 32 | System.out.println(Objects.equals(key, 0L)); 33 | 34 | String defaultAction = "vipgift://com.xmiles.vipgift/mall/ProductDetailActivity?shopType=0&shopParams={\"pageType\":\"2\",\"id\":\"SOURCE_ID_TEMPLATE\"}"; 35 | defaultAction = defaultAction.replace("SOURCE_ID_TEMPLATE", "12345"); 36 | 37 | System.out.println(defaultAction); 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/归并排序/MergeTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba.归并排序; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/3/15 10:44 8 | */ 9 | public class MergeTest { 10 | 11 | private static int[] arrayA = { 23, 47, 81, 95 }; 12 | private static int[] arrayB = { 7, 14, 39, 55, 62, 74 }; 13 | private static int[] arrayC = new int[10]; 14 | 15 | public static void merge() { 16 | int aIndex = 0,bIndex = 0,cIndex = 0; 17 | int aSize = arrayA.length; 18 | int bSize = arrayB.length; 19 | 20 | while (aIndex < aSize && bIndex < bSize) { 21 | if (arrayA[aIndex] < arrayB[bIndex]) { 22 | arrayC[cIndex ++] = arrayA[aIndex++]; 23 | } else { 24 | arrayC[cIndex ++] = arrayB[bIndex ++]; 25 | } 26 | } 27 | 28 | while (aIndex < aSize) { 29 | arrayC[cIndex ++] = arrayA[aIndex++]; 30 | } 31 | 32 | while (bIndex < bSize) { 33 | arrayC[cIndex++] = arrayB[bIndex++]; 34 | } 35 | 36 | for (int i = 0; i < arrayC.length; i++) { 37 | System.out.println(arrayC[i]); 38 | } 39 | } 40 | 41 | public static void main(String[] args) { 42 | merge(); 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/elasticsearch/ResultVo.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.elasticsearch; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import lombok.Data; 5 | 6 | /** 7 | * 统一API响应结果封装 8 | */ 9 | @Data 10 | public class ResultVo { 11 | private final Integer code; 12 | private final String msg; 13 | private final Object data; 14 | 15 | public static class Builder { 16 | private int code; 17 | private String msg; 18 | private Object data; 19 | 20 | public Builder() { 21 | 22 | } 23 | 24 | public Builder code(int code) { 25 | this.code = code; 26 | return this; 27 | } 28 | 29 | public Builder msg(String msg) { 30 | this.msg = msg; 31 | return this; 32 | } 33 | 34 | public Builder data(Object data) { 35 | this.data = data; 36 | return this; 37 | } 38 | 39 | public ResultVo build() { 40 | return new ResultVo(this); 41 | } 42 | } 43 | 44 | private ResultVo(Builder builder) { 45 | this.code = builder.code; 46 | this.msg = builder.msg; 47 | this.data = builder.data; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return JSON.toJSONString(this); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/贪心算法/CoinChange.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba.贪心算法; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/3/2 16:34 8 | */ 9 | 10 | /** 11 | * 贪心算法是指:在每一步求解的步骤中,它要求"贪婪"的选择最佳操作, 12 | * 并希望通过一系列的最佳选择,能够产生一个问题(全局)的最优解。 13 | */ 14 | public class CoinChange { 15 | private static int[] values = {1, 2, 5, 10, 20, 50, 100}; 16 | private static int[] counts = {3, 1, 2, 1, 1, 3, 5}; 17 | 18 | 19 | public static void main(String[] args) { 20 | print(cal(442)); 21 | } 22 | 23 | public static int[] cal(int money) { 24 | int[] result = new int[values.length]; 25 | 26 | for (int i = values.length - 1; i >= 0; i--) { 27 | int c = min(money / values[i], counts[i]); 28 | money = money - c * values[i]; 29 | result[i] = c; 30 | } 31 | return result; 32 | } 33 | 34 | public static int min(int x, int y) { 35 | return x > y ? y : x; 36 | } 37 | 38 | public static void print(int[] result) { 39 | for (int i = values.length - 1; i >= 0; i--) { 40 | if (result[i] != 0) { 41 | System.out.println("需要面额为" 42 | + values[i] + "的人民币的" + result[i] + "张"); 43 | } 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/value/ValueConfig.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.value; 2 | 3 | import com.google.common.collect.Lists; 4 | import lombok.Data; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.time.LocalDateTime; 9 | import java.time.ZoneId; 10 | import java.util.*; 11 | import java.util.stream.Stream; 12 | 13 | /** 14 | * @author xiaoma 15 | * @version V1.0 16 | * @Description: TODO 17 | * @date 2019/5/16 10:37 18 | */ 19 | @Component 20 | @Data 21 | public class ValueConfig { 22 | 23 | @Value("#{'${my.mail}'.split(',')}") 24 | private Collection list; 25 | 26 | public static void main(String[] args) { 27 | LocalDateTime now = LocalDateTime.now(); 28 | System.out.println(Date.from(now.atZone(ZoneId.systemDefault()).toInstant()).toString()); 29 | 30 | System.out.println("============================="); 31 | List integerList = Lists.newArrayList(); 32 | 33 | // 学习lambda 34 | Integer integer = Optional.ofNullable(integerList) 35 | .map(Collection::stream).orElseGet(Stream::empty) 36 | .findFirst() 37 | .map(Integer::intValue).orElseGet(() -> null); 38 | System.out.println("integer=" + integer); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/归并排序/SimpleMergeSort.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba.归并排序; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/3/2 17:32 8 | */ 9 | public class SimpleMergeSort { 10 | 11 | public static int a[] = {1, 3, 5, 7, 9, 11}; 12 | public static int b[] = {2, 4, 6, 8, 10, 12, 14}; 13 | 14 | public static int[] sort() { 15 | int sizeA = a.length; 16 | int sizeB = b.length; 17 | int sizeC = sizeA + sizeB; 18 | int[] c = new int[sizeC]; 19 | 20 | int aIndex = 0, bIndex = 0, cIndex = 0; 21 | 22 | while (aIndex < sizeA && bIndex < sizeB) { 23 | if (a[aIndex] < b[bIndex]) { 24 | c[cIndex++] = a[aIndex++]; 25 | } else { 26 | c[cIndex++] = b[bIndex++]; 27 | } 28 | } 29 | 30 | while (aIndex < sizeA) { 31 | c[cIndex++] = a[aIndex++]; 32 | } 33 | 34 | while (bIndex < sizeB) { 35 | c[cIndex++] = b[bIndex++]; 36 | } 37 | 38 | return c; 39 | } 40 | 41 | public static void print(int[] c) { 42 | for (int i = 0; i < c.length; i++) { 43 | System.out.println(c[i]); 44 | } 45 | } 46 | 47 | public static void main(String[] args) { 48 | print(sort()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/date/ZeroTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.date; 2 | 3 | import com.cmazxiaoma.quzhuanxiang.DateUtils; 4 | 5 | import java.text.ParseException; 6 | import java.time.*; 7 | import java.util.Date; 8 | 9 | /** 10 | * @author xiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2020/2/5 19:32 14 | */ 15 | public class ZeroTest { 16 | 17 | public static void main(String[] args) throws ParseException { 18 | Date currentDate = DateUtils.parseDate("2017-07-20 06:00:00", "yyyy-MM-dd HH:mm:ss"); 19 | 20 | long current = currentDate.getTime(); 21 | 22 | 23 | System.out.println("current:" + current); 24 | 25 | long day = current / (1000 * 24 * 60 * 60); 26 | System.out.println(day); 27 | 28 | long zeroCurrent = day * (1000 * 24 * 60 * 60); 29 | 30 | System.out.println(zeroCurrent); 31 | 32 | System.out.println(current - zeroCurrent); 33 | 34 | 35 | System.out.println(new Date(current - zeroCurrent)); 36 | System.out.println(Date.from(LocalDateTime.of(1970, 01, 01, 0, 0, 0).toInstant(ZoneOffset.ofHours(8)).plusSeconds((current - zeroCurrent) / 1000))); 37 | 38 | 39 | System.out.println(LocalDateTime.of(1970, 01, 01, 0, 0, 0).toInstant(ZoneOffset.ofHours(8)).plusSeconds((current - zeroCurrent) / 1000).toEpochMilli()); 40 | 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/aqs/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.aqs; 2 | 3 | import org.springframework.util.ClassUtils; 4 | import org.springframework.util.ReflectionUtils; 5 | 6 | import java.lang.reflect.Constructor; 7 | import java.lang.reflect.InvocationTargetException; 8 | import java.lang.reflect.Method; 9 | 10 | /** 11 | * @author xiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2019/9/11 10:08 15 | */ 16 | public class Test { 17 | 18 | public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { 19 | long start = System.currentTimeMillis(); 20 | Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis"); 21 | ReflectionUtils.makeAccessible(NO_METHOD_FOUND); 22 | Constructor constructor = System.class.getDeclaredConstructor(); 23 | ReflectionUtils.makeAccessible(constructor); 24 | System system = constructor.newInstance(); 25 | System.out.println(NO_METHOD_FOUND.invoke(system, null)); 26 | long end = System.currentTimeMillis(); 27 | System.out.println(end - start); 28 | long start1 = System.currentTimeMillis(); 29 | long end1 = System.currentTimeMillis(); 30 | System.out.println(end1 - start1); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/elasticsearch/ElasticSearchConfig.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.elasticsearch; 2 | 3 | import org.elasticsearch.client.transport.TransportClient; 4 | import org.elasticsearch.common.settings.Settings; 5 | import org.elasticsearch.common.transport.TransportAddress; 6 | import org.elasticsearch.transport.client.PreBuiltTransportClient; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | import java.net.InetAddress; 11 | import java.net.InetSocketAddress; 12 | import java.net.UnknownHostException; 13 | 14 | /** 15 | * @author cmazxiaoma 16 | * @version V1.0 17 | * @Description: TODO 18 | * @date 2018/10/24 14:40 19 | */ 20 | //@Configuration 21 | //public class ElasticSearchConfig { 22 | // 23 | // @Bean 24 | // public TransportClient client() throws UnknownHostException { 25 | // 26 | // Settings settings = Settings.builder() 27 | // .put("cluster.name", "mayday") 28 | // .build(); 29 | // PreBuiltTransportClient client = new PreBuiltTransportClient(settings); 30 | // int[] localPorts = {9300, 9301, 9302}; 31 | // for (int port : localPorts) { 32 | // client.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.10.6"), port)); 33 | // } 34 | // return client; 35 | // } 36 | //} 37 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/linkedList/LinkedListCreator.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.linkedList; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2018/7/20 9:10 12 | */ 13 | public class LinkedListCreator { 14 | private int count; 15 | /** 16 | * Creates a linked list 17 | * @param data the data to create the list 18 | * @return head of the linked list.The returned linked list 19 | * ends with last node with getNext() == null 20 | */ 21 | public Node createdLinkedList(List data) { 22 | count ++; 23 | 24 | if (data.isEmpty()) { 25 | return null; 26 | } 27 | 28 | Node firstNode = new Node(data.get(0)); 29 | Node headOfSublist = createdLinkedList(data.subList(1, data.size())); 30 | firstNode.setNext(headOfSublist); 31 | return firstNode; 32 | } 33 | 34 | public static void main(String[] args) { 35 | LinkedListCreator creator = new LinkedListCreator(); 36 | Node.prinfLinkedList(creator.createdLinkedList(new ArrayList())); 37 | Node.prinfLinkedList(creator.createdLinkedList(Arrays.asList(1))); 38 | Node.prinfLinkedList(creator.createdLinkedList(Arrays.asList(1, 2, 3, 4, 5))); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/线程安全策略/VectorTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.线程安全策略; 2 | 3 | import java.util.Iterator; 4 | import java.util.Vector; 5 | 6 | /** 7 | * @author cmazxiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2019/2/14 7:14 11 | */ 12 | public class VectorTest { 13 | 14 | private static Vector vector = new Vector<>(); 15 | 16 | public static void main(String[] args) { 17 | 18 | for (;;) { 19 | for (int i = 0; i < 10; i++) { 20 | vector.add(i + 1L); 21 | } 22 | 23 | Thread getThread = new Thread(new Runnable() { 24 | @Override 25 | public void run() { 26 | for (int i = 0; i < vector.size(); i++) { 27 | vector.get(i); 28 | } 29 | } 30 | }); 31 | 32 | Thread removeThread = new Thread(new Runnable() { 33 | @Override 34 | public void run() { 35 | Iterator iterator = vector.iterator(); 36 | while (iterator.hasNext()) { 37 | Long cell = iterator.next(); 38 | iterator.remove(); 39 | } 40 | } 41 | }); 42 | getThread.start(); 43 | removeThread.start(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/quzhuanxiang/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.quzhuanxiang; 2 | 3 | import java.util.Date; 4 | import java.util.concurrent.ThreadLocalRandom; 5 | 6 | /** 7 | * @author xiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2019/9/21 15:44 11 | */ 12 | public class Test { 13 | 14 | public static void main(String[] args) { 15 | System.out.println(100000000L / 225666L); 16 | 17 | Date nowDate = new Date(); 18 | System.out.println(DateUtils.minutesOfTwo(DateUtils.getFirstOfDate(nowDate), nowDate)); 19 | 20 | System.out.println( (int) 0 / 3); 21 | 22 | for (int i = 0; i < 10000; i++) { 23 | Long showCount = 0L; 24 | int minute = DateUtils.minutesOfTwo(DateUtils.getFirstOfDate(nowDate), nowDate); 25 | 26 | int factor = minute / 3; 27 | 28 | Long falseCount = ThreadLocalRandom.current().nextLong( 29 | factor * 80, 30 | factor * 120); 31 | showCount = falseCount; 32 | 33 | if (showCount == 0L) { 34 | showCount = 10000L; 35 | } 36 | // 80 - 120 37 | Long avgMoneyBeanCount = 100000000L / showCount; 38 | System.out.println("minute:" + minute + "," + "factor:" + factor + "," + "falseCount:" + falseCount + ","+ "avgMoneyBeanCount:" + avgMoneyBeanCount); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/thread/ThreadTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.thread; 2 | 3 | import com.cmazxiaoma.InitSpringTest; 4 | import com.cmazxiaoma.hibernate.ISchoolDao; 5 | import com.cmazxiaoma.hibernate.IStudentDao; 6 | import com.cmazxiaoma.mybatis.ISchoolMapper; 7 | import org.junit.Test; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import java.util.concurrent.ThreadPoolExecutor; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | /** 15 | * @author cmazxiaoma 16 | * @version V1.0 17 | * @Description: TODO 18 | * @date 2018/11/12 9:43 19 | */ 20 | public class ThreadTest extends InitSpringTest { 21 | 22 | @Autowired 23 | private ISchoolMapper schoolMapper; 24 | 25 | @Autowired 26 | private IStudentDao studentDao; 27 | 28 | @Test 29 | public void threadTest() throws InterruptedException { 30 | System.out.println("==================start"); 31 | 32 | Thread thread = new Thread(new Runnable() { 33 | @Override 34 | public void run() { 35 | System.out.println("=================thread"); 36 | schoolMapper.findOneByV1("1"); 37 | studentDao.findOne("1"); 38 | } 39 | }); 40 | thread.start(); 41 | thread.join(); 42 | System.out.println("==================end"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/aqs/MyLockTest2.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent.aqs; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | /** 8 | * @author xiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2020/2/13 21:47 12 | */ 13 | public class MyLockTest2 { 14 | 15 | private static List list = new ArrayList<>(); 16 | private static MyLock myLock = new MyLock(); 17 | 18 | public static void main(String[] args) { 19 | Thread t1 = new Thread(new Runnable() { 20 | @Override 21 | public void run() { 22 | for (int i = 0; i < 10000; i++) { 23 | add(i); 24 | } 25 | } 26 | }); 27 | Thread t2 = new Thread(new Runnable() { 28 | @Override 29 | public void run() { 30 | print(); 31 | } 32 | }); 33 | t1.start(); 34 | t2.start(); 35 | } 36 | 37 | private static void add(int i) { 38 | myLock.lock(); 39 | list.add(i); 40 | myLock.unLook(); 41 | } 42 | 43 | private static void print() { 44 | myLock.lock(); 45 | Iterator iterator = list.iterator(); 46 | while (iterator.hasNext()) { 47 | System.out.println(iterator.next()); 48 | } 49 | myLock.unLook(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/netty/demo/juejin/test/ByteBufTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.netty.demo.juejin.test; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | import io.netty.buffer.Unpooled; 5 | 6 | import java.nio.charset.Charset; 7 | 8 | /** 9 | * @author xiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2019/3/18 22:01 13 | */ 14 | public class ByteBufTest { 15 | 16 | public static Charset charset = Charset.forName("UTF-8"); 17 | 18 | public static void main(String[] args) { 19 | ByteBuf byteBuf = Unpooled.copiedBuffer("Hi", charset); 20 | System.out.println("======================================="); 21 | System.out.println(byteBuf.toString(charset)); 22 | System.out.println(byteBuf.maxCapacity()); 23 | System.out.println(byteBuf.readableBytes()); 24 | 25 | 26 | ByteBuf byteBuf1 = byteBuf.slice(); 27 | System.out.println("======================================="); 28 | System.out.println(byteBuf1.toString(charset)); 29 | System.out.println(byteBuf1.maxCapacity()); 30 | System.out.println(byteBuf1.readableBytes()); 31 | 32 | 33 | ByteBuf byteBuf2 = byteBuf.duplicate(); 34 | System.out.println("======================================="); 35 | System.out.println(byteBuf2.toString(charset)); 36 | System.out.println(byteBuf2.maxCapacity()); 37 | System.out.println(byteBuf2.readableBytes()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cvte/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.cvte; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/4/15 21:27 8 | */ 9 | public class BubbleSort { 10 | 11 | private static int[] array = {4,2,1,4,67,656}; 12 | 13 | public static void display() { 14 | for (int i = 0; i < array.length; i++) { 15 | System.out.println(array[i]); 16 | } 17 | } 18 | 19 | public static void sort0() { 20 | for (int i = 0; i < array.length - 1; i++) { 21 | for (int j = 0; j < array.length - i - 1; j++) { 22 | if (array[j] > array[j + 1]) { 23 | swap(j, j + 1); 24 | } 25 | } 26 | } 27 | } 28 | 29 | public static void swap(int a, int b) { 30 | int temp = array[b]; 31 | array[b] = array[a]; 32 | array[a] = temp; 33 | } 34 | 35 | public static void sort1() { 36 | for (int i = array.length - 1; i > 1; i--) { 37 | for (int j = 0; j < i; j++) { 38 | if (array[j] > array[j + 1]) { 39 | swap(j, j + 1); 40 | } 41 | } 42 | } 43 | } 44 | 45 | public static void main(String[] args) { 46 | sort0(); 47 | display(); 48 | System.out.println("==================================="); 49 | sort1(); 50 | display(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/HystrixController.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.concurrent.ExecutionException; 10 | import java.util.concurrent.Future; 11 | 12 | /** 13 | * @author xiaoma 14 | * @version V1.0 15 | * @Description: TODO 16 | * @date 2019/5/31 15:32 17 | */ 18 | @RequestMapping("/hystrix") 19 | @RestController 20 | public class HystrixController { 21 | 22 | @Autowired 23 | private UserService userService; 24 | 25 | @GetMapping("/test0") 26 | public String test0() throws ExecutionException, InterruptedException { 27 | HystrixRequestContext context = HystrixRequestContext.initializeContext(); 28 | Future userFuture1 = userService.getUserByIdV3(1L); 29 | Future userFuture2 = userService.getUserByIdV3(2L); 30 | Future userFuture3 = userService.getUserByIdV3(3L); 31 | 32 | System.out.println(userFuture1.get()); 33 | System.out.println(userFuture2.get()); 34 | System.out.println(userFuture3.get()); 35 | context.close(); 36 | return "ok"; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/gc/FinalizeEscapeGC.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.gc; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/12 10:49 8 | */ 9 | public class FinalizeEscapeGC { 10 | 11 | public static FinalizeEscapeGC SAVE_HOOK = null; 12 | 13 | public void isAlive() { 14 | System.out.println("yes, i am still alive;"); 15 | } 16 | 17 | @Override 18 | protected void finalize() throws Throwable { 19 | super.finalize(); 20 | System.out.println("finalize method executed"); 21 | FinalizeEscapeGC.SAVE_HOOK = this; 22 | } 23 | 24 | public static void main(String[] args) throws InterruptedException { 25 | SAVE_HOOK = new FinalizeEscapeGC(); 26 | SAVE_HOOK = null; 27 | System.gc(); 28 | 29 | // 因为Finalizer方法优先级很低,暂停0.5s,以等待它 30 | Thread.sleep(500); 31 | 32 | if (SAVE_HOOK != null) { 33 | SAVE_HOOK.isAlive(); 34 | } else { 35 | System.out.println("no i am dead"); 36 | } 37 | 38 | System.out.println("================================="); 39 | // 拯救失败 40 | SAVE_HOOK = null; 41 | System.gc(); 42 | 43 | // 因为Finalizer方法优先级很低,暂停0.5s,以等待它 44 | Thread.sleep(500); 45 | 46 | if (SAVE_HOOK != null) { 47 | SAVE_HOOK.isAlive(); 48 | } else { 49 | System.out.println("no i am dead"); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/Test1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | import java.util.Objects; 7 | import java.util.Random; 8 | import java.util.stream.Collectors; 9 | 10 | /** 11 | * @author xiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2019/5/29 16:31 15 | */ 16 | public class Test1 { 17 | 18 | public static void main(String[] args) { 19 | System.out.println(Objects.equals(String.valueOf(12), "12")); 20 | 21 | // List list = null; 22 | // List list2 = list.stream().limit(100).collect(Collectors.toList()); 23 | // System.out.println(list2); 24 | 25 | System.out.println(new Random().nextInt(2)); 26 | System.out.println(new Random().nextInt(2)); 27 | System.out.println(new Random().nextInt(2)); 28 | System.out.println(new Random().nextInt(2)); 29 | System.out.println(new Random().nextInt(2)); 30 | System.out.println(new Random().nextInt(2)); 31 | System.out.println(new Random().nextInt(2)); 32 | System.out.println(new Random().nextInt(2)); 33 | System.out.println(new Random().nextInt(2)); 34 | 35 | // true 36 | System.out.println(!Boolean.FALSE.equals(null)); 37 | // true 38 | System.out.println(!Boolean.FALSE.equals(true)); 39 | // false 40 | System.out.println(!Boolean.FALSE.equals(false)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/Javaoffer/快速排序/QuckTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.Javaoffer.快速排序; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/18 22:56 8 | */ 9 | public class QuckTest { 10 | 11 | public static int[] array = {3123, 43, 4234, 123, 13, 5434, 13, 124, 2112, 10, 5, 1, 0, 424234}; 12 | 13 | public static void sort(int start, int end) { 14 | if (start < end) { 15 | int left = start; 16 | int right = end; 17 | int standrad = array[left]; 18 | 19 | while (left < right) { 20 | while (left < right && array[right] >= standrad) { 21 | right --; 22 | } 23 | 24 | if (left < right) { 25 | array[left ++] = array[right]; 26 | } 27 | 28 | while (left < right && array[left] <= standrad) { 29 | left ++; 30 | } 31 | 32 | if (left < right) { 33 | array[right --] = array[left]; 34 | } 35 | } 36 | 37 | array[left] = standrad; 38 | sort(start, left - 1); 39 | sort(left + 1, end); 40 | } 41 | } 42 | 43 | public static void main(String[] args) { 44 | 45 | sort(0, array.length - 1); 46 | for (int i = 0; i < array.length; i++) { 47 | System.out.println(array[i]); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cpucache/L1CacheMissFast.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.cpucache; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/11/21 12:57 10 | */ 11 | public class L1CacheMissFast { 12 | 13 | private static final int time = 10; 14 | 15 | private static final int row = 1024 * 1024; 16 | 17 | private static final int column = 6; 18 | 19 | // long占8个字节 16 + 8 * 6 + padding = 64个字节 L1d Cache每个缓存块是64Byte 20 | private static long[][] longs; 21 | 22 | public static void main(String[] args) throws Exception { 23 | TimeUnit.SECONDS.sleep(1); 24 | longs = new long[row][]; 25 | 26 | for (int i = 0; i < row; i++) { 27 | longs[i] = new long[column]; 28 | 29 | for (int j = 0; j < column; j++) { 30 | longs[i][j] = 0L; 31 | } 32 | } 33 | System.out.println("starting...."); 34 | 35 | long sum = 0L; 36 | 37 | for (int t = 0; t < time; t++) { 38 | 39 | final long start = System.nanoTime(); 40 | 41 | //fast 42 | for (int i = 0; i < row; i++) { 43 | for (int j = 0; j < column; j++) { 44 | sum += longs[i][j]; 45 | } 46 | } 47 | 48 | System.out.println("t=" + t + ",const=" + (System.nanoTime() - start) + "ns"); 49 | } 50 | 51 | TimeUnit.SECONDS.sleep(1); 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/quzhuanxiang/GameBoxActivityConstant.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.quzhuanxiang; 2 | 3 | import org.apache.commons.lang.time.DateFormatUtils; 4 | import org.apache.commons.lang3.time.DateUtils; 5 | 6 | import java.util.Date; 7 | 8 | /** 9 | * @author xiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2019/9/21 14:51 13 | */ 14 | public class GameBoxActivityConstant { 15 | 16 | public static final String GAME_BOX_ACTIVITY_START_TIME = "2019-10-01 00:00:00"; 17 | 18 | public static final String GAME_BOX_ACTIVITY_END_TIME = "2019-10-11 00:00:00"; 19 | 20 | public static Date GAME_BOX_ACTIVITY_START_DATE; 21 | 22 | public static Date GAME_BOX_ACTIVITY_END_DATE; 23 | 24 | static { 25 | try { 26 | GAME_BOX_ACTIVITY_START_DATE = DateUtils.parseDate(GAME_BOX_ACTIVITY_START_TIME, "yyyy-MM-dd HH:mm:ss"); 27 | GAME_BOX_ACTIVITY_END_DATE = DateUtils.parseDate(GAME_BOX_ACTIVITY_END_TIME, "yyyy-MM-dd HH:mm:ss"); 28 | } catch (Exception ex) { 29 | throw new RuntimeException("初始化GameBoxActivityConstant失败!"); 30 | } 31 | } 32 | 33 | public static void main(String[] args) { 34 | System.out.println(GAME_BOX_ACTIVITY_START_DATE); 35 | System.out.println(GAME_BOX_ACTIVITY_END_DATE); 36 | System.out.println(DateFormatUtils.format(GAME_BOX_ACTIVITY_START_DATE, "yyyy-MM-dd")); 37 | System.out.println(DateFormatUtils.format(GAME_BOX_ACTIVITY_END_DATE, "yyyy-MM-dd")); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/elasticsearch/ResultVoGenerator.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.elasticsearch; 2 | 3 | /** 4 | * 响应结果生成工具 5 | */ 6 | public class ResultVoGenerator { 7 | 8 | public static ResultVo genSuccessResult() { 9 | return new ResultVo.Builder() 10 | .code(ResultCode.SUCCESS.getCode()) 11 | .msg(ResultCode.SUCCESS.getMsg()) 12 | .build(); 13 | } 14 | 15 | public static ResultVo genSuccessResult(Object data) { 16 | return new ResultVo.Builder() 17 | .code(ResultCode.SUCCESS.getCode()) 18 | .msg(ResultCode.SUCCESS.getMsg()) 19 | .data(data) 20 | .build(); 21 | } 22 | 23 | public static ResultVo genFailResult(String message) { 24 | return new ResultVo.Builder() 25 | .code(ResultCode.FAIL.getCode()) 26 | .msg(message) 27 | .build(); 28 | } 29 | 30 | public static ResultVo genCustomResult(ResultCode resultCode) { 31 | return new ResultVo.Builder() 32 | .code(resultCode.getCode()) 33 | .msg(resultCode.getMsg()) 34 | .data("null") 35 | .build(); 36 | } 37 | 38 | public static ResultVo genCustomResult(ResultCode resultCode, String message) { 39 | return new ResultVo.Builder() 40 | .code(resultCode.getCode()) 41 | .msg(message) 42 | .data("null") 43 | .build(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/redis/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.redis; 2 | 3 | import lombok.Data; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.boot.context.properties.ConfigurationProperties; 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2018/5/9 23:44 15 | */ 16 | @Component 17 | @Getter 18 | @Setter 19 | @ConfigurationProperties(prefix = "spring.redis") 20 | public class RedisConfig { 21 | 22 | @Value("${spring.redis.host}") 23 | private String host; 24 | 25 | @Value("${spring.redis.port}") 26 | private int port; 27 | 28 | @Value("${spring.redis.password}") 29 | private String password; 30 | 31 | @Value("${spring.redis.timeout}") 32 | private int timeout; 33 | 34 | @Value("${spring.redis.pool.max-active}") 35 | private int poolMaxActive; 36 | 37 | @Value("${spring.redis.pool.max-idle}") 38 | private int poolMaxIdle; 39 | 40 | @Value("${spring.redis.pool.max-wait}") 41 | private int poolMaxWait; 42 | 43 | @Value("${spring.redis.sentinel.master}") 44 | private String sentinelMaster; 45 | 46 | @Value("${spring.redis.sentinel.nodes}") 47 | private String sentinelNodes; 48 | 49 | @Value("${spring.redis.sentinel.timeout}") 50 | private int sentinelTimeOut; 51 | 52 | @Value("${spring.redis.sentinel.password}") 53 | private String sentinelPassword; 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/2/28 14:16 8 | */ 9 | public class Test { 10 | public static void main(String[] args) { 11 | MyServerChannel myServerChannel = new MyServerChannel(); 12 | myServerChannel.accept(null, new MyHandler(myServerChannel)); 13 | } 14 | 15 | private static class MyHandler implements Handler { 16 | public MyServerChannel myServerChannel; 17 | 18 | public MyHandler(MyServerChannel myServerChannel) { 19 | this.myServerChannel = myServerChannel; 20 | } 21 | 22 | @Override 23 | public void completed(MyClientChannel myClientChannel, Object attachment) { 24 | System.out.println("新连接进入"); 25 | 26 | myServerChannel.accept(null, this); 27 | 28 | myClientChannel.read(null, null, new Handler() { 29 | 30 | @Override 31 | public void completed(Integer result, Object attachment) { 32 | System.out.println("客户端读完成"); 33 | myClientChannel.read(null, null, this); 34 | } 35 | 36 | @Override 37 | public void failed(Throwable ex, Object attachment) { 38 | 39 | } 40 | 41 | }); 42 | 43 | } 44 | 45 | @Override 46 | public void failed(Throwable ex, Object attachment) { 47 | 48 | } 49 | } 50 | } 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/config/FilterConfig.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.config; 2 | 3 | import org.springframework.boot.web.servlet.FilterRegistrationBean; 4 | import org.springframework.boot.web.servlet.ServletRegistrationBean; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.orm.hibernate5.support.OpenSessionInViewFilter; 8 | import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2018/7/4 9:49 15 | */ 16 | @Configuration 17 | public class FilterConfig { 18 | 19 | /** 20 | * 解决hibernate懒加载出现的no session问题 21 | * @return 22 | */ 23 | // @Bean 24 | // public FilterRegistrationBean filterRegistrationBean() { 25 | // FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); 26 | // filterRegistrationBean.setFilter(new OpenSessionInViewFilter()); 27 | // filterRegistrationBean.addInitParameter("urlPatterns", "/*"); 28 | // return filterRegistrationBean; 29 | // } 30 | 31 | /** 32 | * 解决jpa 懒加载出现的no session问题 33 | * @return 34 | */ 35 | @Bean 36 | public FilterRegistrationBean filterRegistrationBean() { 37 | FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); 38 | filterRegistrationBean.setFilter(new OpenEntityManagerInViewFilter()); 39 | filterRegistrationBean.addInitParameter("urlPatterns", "/*"); 40 | return filterRegistrationBean; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/VolatileTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent; 2 | 3 | import java.util.concurrent.CountDownLatch; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/11/13 17:49 10 | */ 11 | public class VolatileTest { 12 | 13 | private static int x = 0, y = 0; 14 | private static int a = 0, b =0; 15 | 16 | public static void main(String[] args) throws InterruptedException { 17 | int i = 0; 18 | for(;;) { 19 | i++; 20 | x = 0; y = 0; 21 | a = 0; b = 0; 22 | CountDownLatch latch = new CountDownLatch(1); 23 | 24 | Thread one = new Thread(() -> { 25 | try { 26 | latch.await(); 27 | } catch (InterruptedException e) { 28 | } 29 | a = 1; 30 | x = b; 31 | }); 32 | 33 | Thread other = new Thread(() -> { 34 | try { 35 | latch.await(); 36 | } catch (InterruptedException e) { 37 | } 38 | b = 1; 39 | y = a; 40 | }); 41 | one.start();other.start(); 42 | latch.countDown(); 43 | one.join();other.join(); 44 | 45 | String result = "第" + i + "次 (" + x + "," + y + ")"; 46 | if(x == 0 && y == 0) { 47 | System.err.println(result); 48 | break; 49 | } else { 50 | System.out.println(result); 51 | } 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/netty/demo/EchoServerHandler.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.netty.demo; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | import io.netty.buffer.Unpooled; 5 | import io.netty.channel.ChannelFutureListener; 6 | import io.netty.channel.ChannelHandler; 7 | import io.netty.channel.ChannelHandlerContext; 8 | import io.netty.channel.ChannelInboundHandlerAdapter; 9 | import io.netty.util.CharsetUtil; 10 | 11 | /** 12 | * @author cmazxiaoma 13 | * @version V1.0 14 | * @Description: TODO 15 | * @date 2018/12/6 13:57 16 | */ 17 | //标记一个ChannelHandler可以被多个Channel安全的共享 18 | @ChannelHandler.Sharable 19 | public class EchoServerHandler extends ChannelInboundHandlerAdapter { 20 | 21 | @Override 22 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 23 | ByteBuf in = (ByteBuf) msg; 24 | System.out.println("Server received :" + in.toString(CharsetUtil.UTF_8)); 25 | 26 | // 将接收到的消息写给发送者, 而不冲刷出站消息 27 | ctx.write(in); 28 | } 29 | 30 | @Override 31 | public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { 32 | /** 33 | * 未决消息(pending message)是指目前暂存于ChannelOutboundBuffer中的消息,下一次调用 34 | * flush()或者writeAndFlush()方法时将会尝试写出到套接字。 35 | * 36 | * 将未决消息冲刷到远程节点,并且关闭该Channel 37 | */ 38 | ctx.writeAndFlush(Unpooled.EMPTY_BUFFER); 39 | //.addListener(ChannelFutureListener.CLOSE); 40 | } 41 | 42 | @Override 43 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 44 | cause.printStackTrace(); 45 | ctx.close(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/spring/bean/SpringTestBean.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring.bean; 2 | 3 | import com.cmazxiaoma.spring.lazy.Hero; 4 | import org.springframework.beans.BeansException; 5 | import org.springframework.beans.factory.BeanFactory; 6 | import org.springframework.beans.factory.BeanFactoryAware; 7 | import org.springframework.beans.factory.BeanNameAware; 8 | import org.springframework.beans.factory.config.BeanDefinition; 9 | import org.springframework.stereotype.Component; 10 | 11 | import java.util.Map; 12 | 13 | /** 14 | * @author cmazxiaoma 15 | * @version V1.0 16 | * @Description: TODO 17 | * @date 2018/8/7 20:01 18 | */ 19 | public class SpringTestBean implements BeanNameAware, BeanFactoryAware { 20 | 21 | private BeanFactory beanFactory; 22 | 23 | private String beanName; 24 | 25 | @Override 26 | public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 27 | this.beanFactory = beanFactory; 28 | // System.out.println("beanFactory=" + this.beanFactory); 29 | 30 | // Object object = beanFactory.getBean(SpringTest3.class); 31 | // System.out.println("SpringTest3=" + object); 32 | 33 | // Object hero = beanFactory.getBean(Hero.class); 34 | // System.out.println("hero=" + hero); 35 | } 36 | 37 | @Override 38 | public void setBeanName(String name) { 39 | this.beanName = name; 40 | // System.out.println("beanName=" + this.beanName); 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "SpringTestBean{" + 46 | "beanName='" + beanName + '\'' + 47 | '}'; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cpucache/L1CacheMiss1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.cpucache; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/12/13 15:13 8 | */ 9 | public class L1CacheMiss1 { 10 | private static final int RUNS = 10; 11 | private static final int DIMENSION_1 = 1024 * 1024; 12 | private static final int DIMENSION_2 = 62; 13 | 14 | private static long[][] longs; 15 | 16 | /** 17 | * 7516230000 18 | * 605615600 19 | * @param args 20 | * @throws Exception 21 | */ 22 | public static void main(String[] args) throws Exception { 23 | Thread.sleep(10000); 24 | longs = new long[DIMENSION_1][]; 25 | for (int i = 0; i < DIMENSION_1; i++) { 26 | longs[i] = new long[DIMENSION_2]; 27 | for (int j = 0; j < DIMENSION_2; j++) { 28 | longs[i][j] = 0L; 29 | } 30 | } 31 | System.out.println("starting...."); 32 | 33 | final long start = System.nanoTime(); 34 | long sum = 0L; 35 | for (int r = 0; r < RUNS; r++) { 36 | // for (int j = 0; j < DIMENSION_2; j++) { 37 | // for (int i = 0; i < DIMENSION_1; i++) { 38 | // sum += longs[i][j]; 39 | // } 40 | // } 41 | 42 | for (int i = 0; i < DIMENSION_1; i++) { 43 | for (int j = 0; j < DIMENSION_2; j++) { 44 | sum += longs[i][j]; 45 | } 46 | } 47 | } 48 | 49 | System.out.println(sum); 50 | System.out.println("duration = " + (System.nanoTime() - start)); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cpucache/L1CacheMissSlow.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.cpucache; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** 6 | * @author cmazxiaoma 7 | * @version V1.0 8 | * @Description: TODO 9 | * @date 2018/11/21 12:57 10 | */ 11 | public class L1CacheMissSlow { 12 | 13 | private static final int time = 10; 14 | 15 | private static final int row = 1024 * 1024; 16 | 17 | private static final int column = 6; 18 | 19 | // long占8个字节 20 | private static long[][] longs; 21 | 22 | public static void main(String[] args) throws Exception { 23 | TimeUnit.SECONDS.sleep(1); 24 | longs = new long[row][]; 25 | 26 | for (int i = 0; i < row; i++) { 27 | longs[i] = new long[column]; 28 | 29 | for (int j = 0; j < column; j++) { 30 | longs[i][j] = 0L; 31 | } 32 | } 33 | System.out.println("starting...."); 34 | 35 | long sum = 0L; 36 | 37 | for (int t = 0; t < time; t++) { 38 | 39 | final long start = System.nanoTime(); 40 | 41 | //slow 42 | for (int j = 0; j < column; j++) { 43 | for (int i = 0; i < row; i++) { 44 | sum += longs[i][j]; 45 | } 46 | } 47 | 48 | //fast 49 | // for (int i = 0; i < row; i++) { 50 | // for (int j = 0; j < column; j++) { 51 | // sum += longs[i][j]; 52 | // } 53 | // } 54 | System.out.println("t=" + t + ",const=" + (System.nanoTime() - start) + "ns"); 55 | } 56 | 57 | TimeUnit.SECONDS.sleep(1); 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/elasticsearch/ResultCode.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.elasticsearch; 2 | 3 | /** 4 | * 响应码枚举,参考HTTP状态码的语义 5 | */ 6 | public enum ResultCode { 7 | //成功 8 | SUCCESS(200, "success"), 9 | 10 | //失败 11 | FAIL(400, "fail"), 12 | 13 | //未认证(签名错误) 14 | UNAUTHORIZED(401, "unauthorized"), 15 | 16 | //接口不存在 17 | NOT_FOUND(404, "not found"), 18 | 19 | //服务器内部错误 20 | INTERNAL_SERVER_ERROR(500, "internal_server_error"), 21 | 22 | //拒接访问 23 | FORBIDDEN(401, "您没有权限访问该资源"), 24 | 25 | //账号错误 26 | USERNAME_ERROR(1, "对不起,您的账号错误,请检查后再登陆!"), 27 | 28 | //账号被禁用,请联系管理员 29 | ACCOUNT_DISABLED(2, "账号被禁用,请联系管理员"), 30 | 31 | //登录失败 32 | LOGIN_FAILED(3, "登录失败"), 33 | 34 | //非法参数 35 | ILLEGAL_PARAMETERS(4, "非法参数"), 36 | 37 | //查询无记录 38 | QUERY_NO_DATA(5, "查询无信息"), 39 | 40 | PASSWORD_ERROR(6, "对不起,您的密码错误,请检查后再登陆!"), 41 | 42 | ACCOUNT_LOCAKED(7, "账号被锁定, 请联系管理员"), 43 | 44 | //未知错误 45 | UNKNOWN_ERROR(8, "未知错误"), 46 | 47 | //Session失效 48 | INVALID_SESSION(9, "session已失效"), 49 | 50 | ACCOUNT_LOGIN_ELSEWHERE(10, "账号已在其他地方登录,被挤下线!"), 51 | 52 | REQUEST_METHOD_ERROR(11, "请求方式错误"), 53 | 54 | ARGUMENT_TYPE_MISMATCH_ERROR(12, "参数类型不匹配!"), 55 | 56 | REQUIRED_PARAM_EMPTY(13, "必选参数为空!"), 57 | 58 | PARAM_FORMAT_ERROR(14, "参数格式错误!"); 59 | 60 | 61 | private Integer code; 62 | 63 | private String msg; 64 | 65 | ResultCode(Integer code, String msg) { 66 | this.code = code; 67 | this.msg = msg; 68 | } 69 | 70 | public Integer getCode() { 71 | return code; 72 | } 73 | 74 | public String getMsg() { 75 | return msg; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/慕课网并发学习/Hystrix/HystrixController1.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.慕课网并发学习.Hystrix; 2 | 3 | import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; 4 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 5 | import javafx.beans.DefaultProperty; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2019/2/17 12:41 15 | */ 16 | @RestController 17 | @RequestMapping("/hystrix1") 18 | @DefaultProperties(defaultFallback = "defaultFail") 19 | public class HystrixController1 { 20 | 21 | @HystrixCommand(fallbackMethod = "fail1") 22 | @GetMapping("/test1") 23 | public String test1() { 24 | throw new RuntimeException(); 25 | } 26 | 27 | private String fail1() { 28 | System.out.println("fail1"); 29 | return "fail1"; 30 | } 31 | 32 | @HystrixCommand(fallbackMethod = "fail2") 33 | @GetMapping("/test2") 34 | public String test2() { 35 | throw new RuntimeException(); 36 | } 37 | 38 | @HystrixCommand(fallbackMethod = "fail3") 39 | private String fail2() { 40 | System.out.println("fail2"); 41 | throw new RuntimeException(); 42 | } 43 | 44 | @HystrixCommand 45 | private String fail3() { 46 | System.out.println("fail3"); 47 | throw new RuntimeException(); 48 | } 49 | 50 | private String defaultFail() { 51 | System.out.println("default fail"); 52 | return "default fail"; 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/HibernateConfig.java: -------------------------------------------------------------------------------- 1 | //package com.cmazxiaoma; 2 | // 3 | //import org.apache.ibatis.session.SqlSessionFactory; 4 | //import org.hibernate.SessionFactory; 5 | //import org.hibernate.boot.spi.SessionFactoryBuilderFactory; 6 | //import org.springframework.beans.factory.annotation.Autowired; 7 | //import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 8 | //import org.springframework.context.annotation.Bean; 9 | //import org.springframework.context.annotation.Configuration; 10 | //import org.springframework.core.annotation.Order; 11 | //import org.springframework.orm.hibernate4.HibernateTemplate; 12 | //import org.springframework.orm.hibernate5.LocalSessionFactoryBean; 13 | //import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean; 14 | //import org.springframework.transaction.annotation.EnableTransactionManagement; 15 | // 16 | //import javax.annotation.Resource; 17 | //import javax.persistence.EntityManagerFactory; 18 | //import javax.sql.DataSource; 19 | // 20 | ///** 21 | // * @author cmazxiaoma 22 | // * @version V1.0 23 | // * @Description: TODO 24 | // * @date 2018/9/21 17:30 25 | // */ 26 | //@Configuration 27 | //@EnableAutoConfiguration 28 | //@EnableTransactionManagement 29 | //public class HibernateConfig { 30 | // 31 | // @Autowired 32 | // private EntityManagerFactory entityManagerFactory; 33 | // 34 | // @Bean(name = "sessionFactory") 35 | // public SessionFactory sessionFactory() { 36 | // if (entityManagerFactory.unwrap(SessionFactory.class) == null) { 37 | // throw new NullPointerException("factory is not hibernate factory"); 38 | // } 39 | // return entityManagerFactory.unwrap(SessionFactory.class); 40 | // } 41 | //} 42 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/Test3.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author xiaoma 8 | * @version V1.0 9 | * @Description: TODO 10 | * @date 2019/12/30 17:35 11 | */ 12 | public class Test3 { 13 | 14 | public static void main(String[] args) { 15 | method4(); 16 | } 17 | 18 | public static void method4() { 19 | ArrayList list = new ArrayList<>(); 20 | list.add(1); 21 | list.add(2); 22 | list.add(3); 23 | for(int i : list){ 24 | System.out.println(i); 25 | if(i == 3){ 26 | list.remove((Object)3); 27 | } 28 | } 29 | } 30 | 31 | public static void method1() { 32 | List a = new ArrayList(); 33 | a.add("1"); 34 | a.add("2"); 35 | for (String temp : a) { 36 | 37 | if("1".equals(temp)){ 38 | a.remove(temp); 39 | } 40 | } 41 | } 42 | 43 | public static void method2() { 44 | List a = new ArrayList(); 45 | a.add("1"); 46 | a.add("2"); 47 | for (String temp : a) { 48 | if("2".equals(temp)){ 49 | a.remove(temp); 50 | } 51 | } 52 | } 53 | 54 | 55 | public static void method3() { 56 | List a = new ArrayList(); 57 | a.add("1"); 58 | a.add("2"); 59 | for (String temp : a) { 60 | System.out.println(temp); 61 | if ("2".equals(temp)) { 62 | a.add("3"); 63 | a.remove("2"); 64 | } 65 | } 66 | System.out.println(a); 67 | } 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/extendsuper/extendSuper.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.extendsuper; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author cmazxiaoma 8 | * @version V1.0 9 | * @Description: 10 | * @date 2018/7/10 11:54 11 | */ 12 | public class extendSuper { 13 | 14 | public static void main(String[] args) { 15 | List fruitList = new ArrayList<>(); 16 | // fruitList.add(new Apple()); 17 | // fruitList.add(new Orange()); 18 | 19 | // fruitList.add(new Fruit()); 20 | // fruitList.add(new Thing()); 21 | 22 | fruitList.add(null); 23 | fruitList.contains(new Apple()); 24 | fruitList.indexOf(new Apple()); 25 | 26 | Fruit fruit = fruitList.get(0); 27 | Orange orange = (Orange) fruitList.get(0); 28 | 29 | System.out.println("fruit=" + fruit); 30 | System.out.println("orange=" + orange); 31 | 32 | List fruitList1 = new ArrayList<>(); 33 | fruitList1.add(new Apple()); 34 | fruitList1.add(new Apple1()); 35 | // fruitList1.add(new Fruit()); 36 | 37 | Apple apple1 = (Apple) fruitList1.get(0); 38 | Apple apple2 = (Apple) fruitList1.get(0); 39 | System.out.println("apple1=" + apple1); 40 | System.out.println("apple2=" + apple2); 41 | } 42 | } 43 | class Thing {} 44 | class Fruit extends Thing {} 45 | class Apple extends Fruit {} 46 | class Orange extends Fruit {} 47 | class Apple1 extends Apple {} 48 | 49 | 50 | /** 51 | * 52 | * add:不允许添加任何元素 53 | * get:可以获取元素,但是必须使用E来接收元素 54 | * 55 | * 56 | * add:可以添加E和E的子类元素 57 | * get:可以获取元素,但是类的信息丢失了,所以返回只能使用Object引用来接收。 58 | * 如果需要自己类型需要强制转换。 59 | */ 60 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/UserCommand.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | import com.netflix.hystrix.*; 4 | import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault; 5 | 6 | import java.util.concurrent.TimeUnit; 7 | 8 | /** 9 | * @author xiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2019/5/31 19:44 13 | */ 14 | public class UserCommand extends HystrixCommand { 15 | 16 | public UserCommand() { 17 | super(Setter.withGroupKey(HystrixCommandGroupKey 18 | .Factory.asKey("UserGroup")).andCommandKey( 19 | HystrixCommandKey.Factory.asKey("UserCommand")) 20 | .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("UserThreadPool")) 21 | ); 22 | } 23 | 24 | public UserCommand(HystrixCommandGroupKey group) { 25 | super(group); 26 | } 27 | 28 | @Override 29 | protected HystrixTest.User run() throws Exception { 30 | TimeUnit.SECONDS.sleep(2); 31 | System.out.println("=========执行完毕...============="); 32 | return new HystrixTest.User().setId(1L); 33 | } 34 | 35 | /** 36 | * fallback方法修饰符没有特定的要求,定义成public,private,protected 37 | * @return 38 | */ 39 | @Override 40 | protected HystrixTest.User getFallback() { 41 | return new HystrixTest.User().setId(1L).setMsg("fallback"); 42 | } 43 | 44 | @Override 45 | protected String getCacheKey() { 46 | return String.valueOf(1L); 47 | } 48 | 49 | // 清除缓存 50 | public static void flushCache(String cacheKey) { 51 | HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("UserCommand"), 52 | HystrixConcurrencyStrategyDefault.getInstance() 53 | ).clear(cacheKey); 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/hystrix/BeanCopierTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.hystrix; 2 | 3 | 4 | import org.assertj.core.util.Lists; 5 | import org.springframework.beans.BeanUtils; 6 | import org.springframework.cglib.beans.BeanCopier; 7 | 8 | import java.util.Collection; 9 | import java.util.List; 10 | 11 | /** 12 | * @author xiaoma 13 | * @version V1.0 14 | * @Description: TODO 15 | * @date 2019/6/3 16:07 16 | */ 17 | public class BeanCopierTest { 18 | 19 | public static void main(String[] args) throws InstantiationException, IllegalAccessException { 20 | Admin admin = new Admin().setId(1L).setName("cmazxiaoma"); 21 | List adminList = Lists.newArrayList(); 22 | adminList.add(admin); 23 | adminList.add(new Admin().setId(2L).setName("cmazxiaoma1")); 24 | 25 | List adminDtoList = Lists.newArrayList(); 26 | 27 | adminDtoList = convertDTO(adminList, Admin.class, AdminDto.class); 28 | System.out.println(adminDtoList); 29 | } 30 | 31 | 32 | public static List convertDTO(Collection sourceList, 33 | Class sourceClass, 34 | Class destinationClass) 35 | throws IllegalAccessException, InstantiationException { 36 | 37 | BeanCopier beanCopier = BeanCopier.create( 38 | sourceClass, 39 | destinationClass, 40 | false 41 | ); 42 | 43 | List destinationList = Lists.newArrayList(); 44 | for (Object sourceObject : sourceList) { 45 | D instance = destinationClass.newInstance(); 46 | beanCopier.copy(sourceObject, instance, null); 47 | destinationList.add(instance); 48 | } 49 | return destinationList; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/java/Test3.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.java; 2 | 3 | /** 4 | * @author cmazxiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2018/11/16 23:16 8 | */ 9 | public class Test3 { 10 | 11 | public static void main(String[] args) { 12 | new Leaf(); 13 | new Leaf(); 14 | /* 15 | 16 | Root类的静态初始块 17 | Mid的静态初始块 18 | Leaf的静态初始化块 19 | Root类的普通初始化块 20 | Root类的无参数的构造器 21 | Mid的普通初始块 22 | Mid的无参数的构造器 23 | Mid的有参数的构造器,参数= 烟火跟我都是越黑越灿烂 24 | Leaf的普通的初始化块 25 | Leaf的无参数的构造器 26 | 27 | Root类的普通初始化块 28 | Root类的无参数的构造器 29 | Mid的普通初始块 30 | Mid的无参数的构造器 31 | Mid的有参数的构造器,参数= 烟火跟我都是越黑越灿烂 32 | Leaf的普通的初始化块 33 | Leaf的无参数的构造器 34 | */ 35 | } 36 | 37 | } 38 | 39 | class Root { 40 | static { 41 | System.out.println("Root类的静态初始块"); 42 | } 43 | 44 | { 45 | System.out.println("Root类的普通初始化块"); 46 | } 47 | 48 | public Root() { 49 | System.out.println("Root类的无参数的构造器"); 50 | } 51 | } 52 | 53 | class Mid extends Root { 54 | static { 55 | System.out.println("Mid的静态初始块"); 56 | } 57 | 58 | { 59 | System.out.println("Mid的普通初始块"); 60 | } 61 | 62 | public Mid() { 63 | System.out.println("Mid的无参数的构造器"); 64 | } 65 | 66 | public Mid(String msg) { 67 | this(); 68 | System.out.println("Mid的有参数的构造器,参数=" + msg); 69 | } 70 | } 71 | 72 | class Leaf extends Mid { 73 | static { 74 | System.out.println("Leaf的静态初始化块"); 75 | } 76 | 77 | { 78 | System.out.println("Leaf的普通的初始化块"); 79 | } 80 | 81 | public Leaf() { 82 | super("烟火跟我都是越黑越灿烂"); 83 | System.out.println("Leaf的无参数的构造器"); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/cvte/MergeSort.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.cvte; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/4/15 11:51 8 | */ 9 | public class MergeSort { 10 | private static int[] array = {4,2,1,4,67,656}; 11 | 12 | 13 | public static void recMergeSort(int[] workSpace, int lowBound, int upperBound) { 14 | if (lowBound < upperBound) { 15 | int mid = (lowBound + upperBound) / 2; 16 | recMergeSort(workSpace, lowBound, mid); 17 | recMergeSort(workSpace, mid + 1, upperBound); 18 | mergeSort(workSpace, lowBound, mid + 1, upperBound); 19 | } 20 | } 21 | 22 | public static void mergeSort(int[] workSpace, int lowStr, int highStr, int upperBound) { 23 | int j = 0; 24 | int mid = highStr - 1; 25 | int lowBound = lowStr; 26 | int n = upperBound - lowBound + 1; 27 | 28 | while (lowStr <= mid && highStr <= upperBound) { 29 | if (array[lowStr] < array[highStr]) { 30 | workSpace[j++] = array[lowStr++]; 31 | } else { 32 | workSpace[j++] = array[highStr++]; 33 | } 34 | } 35 | 36 | while (lowStr <= mid) { 37 | workSpace[j++] = array[lowStr++]; 38 | } 39 | 40 | while (highStr <= upperBound) { 41 | workSpace[j++] = array[highStr++]; 42 | } 43 | 44 | for (j = 0; j < n; j++) { 45 | array[j + lowBound] = workSpace[j]; 46 | } 47 | } 48 | 49 | public static void main(String[] args){ 50 | recMergeSort(new int[array.length], 0, array.length - 1); 51 | display(); 52 | } 53 | 54 | public static void display() { 55 | for (int i = 0; i < array.length; i++) { 56 | System.out.println(array[i]); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/lambda/Test3.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.lambda; 2 | 3 | import com.google.common.collect.*; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | import java.util.Objects; 8 | import java.util.Set; 9 | import java.util.stream.Collector; 10 | import java.util.stream.Collectors; 11 | 12 | /** 13 | * @author xiaoma 14 | * @version V1.0 15 | * @Description: TODO 16 | * @date 2020/1/7 17:55 17 | */ 18 | public class Test3 { 19 | 20 | public static void main(String[] args) { 21 | List list = Lists.newArrayList(); 22 | list.add(new PhonePrdDto("dc03debda990e301", "14000", -1L)); 23 | list.add(new PhonePrdDto("dc03debda990e301", "14300", -1L)); 24 | list.add(new PhonePrdDto("dc03debda990e301", "14400", -1L)); 25 | 26 | list.add(new PhonePrdDto("dc03debda990e301", "14000", -1L)); 27 | list.add(new PhonePrdDto("dc03debda990e301", "14000", 1L)); 28 | list.add(new PhonePrdDto("dc03debda990e301", "14000", 2L)); 29 | list.add(new PhonePrdDto("dc03debda990e301", "14000", null)); 30 | 31 | Map> prdMap = list.stream().collect(Collectors.groupingBy(PhonePrdDto::getPrdId, Collectors.mapping(PhonePrdDto::getPhoneId, Collectors.toSet()))); 32 | 33 | 34 | SetMultimap setMultimap = HashMultimap.create(); 35 | 36 | list.forEach(fruit -> { 37 | if (!Objects.isNull(fruit.getUserId()) && fruit.getUserId() > 0L) { 38 | setMultimap.put(fruit.getPhoneId() + "_" + fruit.getPrdId(), fruit.getUserId()); 39 | } 40 | }); 41 | 42 | System.out.println("prdMap:" + prdMap); 43 | System.out.println("multiMap:" + setMultimap); 44 | 45 | Set userIdList = setMultimap.get("dc03debda990e301_14300"); 46 | System.out.println("userIdList:" + userIdList); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/Javaoffer/classLoader/MyClassLoader.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.Javaoffer.classLoader; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.InputStream; 7 | 8 | /** 9 | * @author cmazxiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2019/2/18 21:44 13 | */ 14 | public class MyClassLoader extends ClassLoader { 15 | 16 | private String path; 17 | private String classLoaderName; 18 | 19 | public MyClassLoader(String path, String classLoaderName) { 20 | this.path = path; 21 | this.classLoaderName = classLoaderName; 22 | } 23 | 24 | @Override 25 | protected Class findClass(String name) throws ClassNotFoundException { 26 | byte[] b = loadClassData(name); 27 | return defineClass(name, b, 0, b.length); 28 | } 29 | 30 | /** 31 | * 用于加载类文件 32 | * @param name 33 | * @return 34 | */ 35 | private byte[] loadClassData(String name) { 36 | name = path + name + ".class"; 37 | InputStream in = null; 38 | ByteArrayOutputStream out = null; 39 | 40 | try { 41 | in = new FileInputStream(new File(name)); 42 | out = new ByteArrayOutputStream(); 43 | int i = 0; 44 | 45 | while ((i = in.read()) != -1) { 46 | out.write(i); 47 | } 48 | } catch (Exception ex) { 49 | ex.printStackTrace(); 50 | } finally { 51 | try { 52 | if (out != null) { 53 | out.close(); 54 | } 55 | 56 | if (in != null) { 57 | in.close(); 58 | } 59 | } catch (Exception ex) { 60 | ex.printStackTrace(); 61 | } 62 | } 63 | 64 | return out.toByteArray(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/归并排序/Merge.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba.归并排序; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: TODO 7 | * @date 2019/3/15 11:02 8 | */ 9 | public class Merge { 10 | 11 | private static int[] array = {4,2,1,4,67,656}; 12 | 13 | public static void recMergeSort(int[] workSpace, int lowerBound, int upperBound) { 14 | if (lowerBound == upperBound) { 15 | 16 | return; 17 | } else { 18 | int mid = (lowerBound + upperBound) / 2; 19 | recMergeSort(workSpace, lowerBound, mid); 20 | recMergeSort(workSpace, mid + 1, upperBound); 21 | merge(workSpace, lowerBound, mid + 1, upperBound); 22 | } 23 | } 24 | 25 | public static void merge(int[] workSpace, int lowPtr, int highPtr, int upperBound) { 26 | int j = 0; 27 | int lowerBound = lowPtr; 28 | int mid = highPtr - 1; 29 | int n = upperBound - lowerBound + 1; 30 | 31 | while (lowPtr <= mid && highPtr <= upperBound) { 32 | if (array[lowPtr] < array[highPtr]) { 33 | workSpace[j++] = array[lowPtr++]; 34 | } else { 35 | workSpace[j++] = array[highPtr++]; 36 | } 37 | } 38 | 39 | while (lowPtr <= mid) { 40 | workSpace[j++] = array[lowPtr++]; 41 | } 42 | 43 | while (highPtr <= upperBound) { 44 | workSpace[j++] = array[highPtr++]; 45 | } 46 | 47 | for (j = 0; j < n; j++) { 48 | array[lowerBound + j] = workSpace[j]; 49 | } 50 | } 51 | 52 | public static void main(String[] args) { 53 | recMergeSort(new int[array.length], 0, array.length - 1); 54 | display(); 55 | } 56 | 57 | public static void display() { 58 | for (int i = 0; i < array.length; i++) { 59 | System.out.println(array[i]); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/redis/RedisTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.redis; 2 | 3 | import com.cmazxiaoma.InitSpringTest; 4 | import org.junit.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import redis.clients.jedis.*; 7 | 8 | /** 9 | * @author cmazxiaoma 10 | * @version V1.0 11 | * @Description: TODO 12 | * @date 2018/9/10 16:06 13 | */ 14 | public class RedisTest extends InitSpringTest { 15 | 16 | @Autowired 17 | private JedisSentinelPool jedisSentinelPool; 18 | 19 | @Autowired 20 | private JedisPool jedisPool; 21 | 22 | @Test 23 | public void testJedisSentinelPool() { 24 | HostAndPort hostAndPort = jedisSentinelPool.getCurrentHostMaster(); 25 | 26 | System.out.println("=====>hostAndPort=" + hostAndPort); 27 | 28 | Jedis jedis = null; 29 | 30 | try { 31 | jedis = jedisSentinelPool.getResource(); 32 | jedis.del("name"); 33 | jedis.set("name", "SUCCESS"); 34 | } finally { 35 | if (jedis != null) { 36 | jedis.close(); 37 | } 38 | } 39 | } 40 | 41 | @Test 42 | public void testJedisPool() { 43 | Jedis jedis = null; 44 | try { 45 | jedis = jedisPool.getResource(); 46 | jedis.del("name"); 47 | jedis.set("name", "jedisSentinel"); 48 | } finally { 49 | if (jedis != null) { 50 | jedis.close(); 51 | } 52 | } 53 | } 54 | 55 | @Test 56 | public void testKeys() { 57 | Jedis jedis = null; 58 | 59 | try { 60 | jedis = jedisPool.getResource(); 61 | 62 | for (int i = 0; i < 1000000; i++) { 63 | jedis.hset("myhashmap", i + "", i + ""); 64 | } 65 | } finally { 66 | if (jedis != null) { 67 | jedis.close(); 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/com/cmazxiaoma/spring/SpringTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.spring; 2 | 3 | import com.cmazxiaoma.InitSpringTest; 4 | import com.cmazxiaoma.spring.bean.SpringTest2; 5 | import com.cmazxiaoma.spring.bean.SpringTest3; 6 | import com.cmazxiaoma.spring.bean.SpringTestBean; 7 | import com.cmazxiaoma.spring.compoent.SpringTest4; 8 | import com.cmazxiaoma.spring.lazy.Hero; 9 | import com.cmazxiaoma.spring.multipleBeans.IBeanService; 10 | import org.junit.Test; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.context.ApplicationContext; 13 | 14 | import java.util.Map; 15 | 16 | /** 17 | * @author cmazxiaoma 18 | * @version V1.0 19 | * @Description: TODO 20 | * @date 2018/8/8 16:16 21 | */ 22 | public class SpringTest extends InitSpringTest { 23 | 24 | @Autowired 25 | private SpringTest3 springTest3; 26 | 27 | @Autowired 28 | private SpringTestBean springTestBean; 29 | 30 | private Hero hero; 31 | 32 | @Autowired 33 | private SpringTest4 springTest4; 34 | 35 | @Autowired 36 | private SpringTest2 springTest2; 37 | 38 | @Autowired 39 | private ApplicationContext applicationContext; 40 | 41 | @Test 42 | public void test() { 43 | System.out.println("springTest3=" + springTest3); 44 | } 45 | 46 | @Test 47 | public void test1() { 48 | System.out.println("springTestBean=" + springTestBean); 49 | } 50 | 51 | @Test 52 | public void testHero() { 53 | hero = SpringApplicationContext.getBeanByType(Hero.class); 54 | } 55 | 56 | @Test 57 | public void test4() { 58 | System.out.println("springTest4=" + springTest4); 59 | } 60 | 61 | @Test 62 | public void test5() { 63 | System.out.println(springTest2.toString()); 64 | } 65 | 66 | @Test 67 | public void test6() { 68 | System.out.println("beanService" + applicationContext.getBeansOfType(IBeanService.class)); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/controller/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.controller.v1; 2 | 3 | import com.cmazxiaoma.concurrent.CustomThreadPoolExecutor; 4 | import com.cmazxiaoma.controller.HttpUtil; 5 | 6 | import java.util.concurrent.ArrayBlockingQueue; 7 | import java.util.concurrent.CountDownLatch; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2018/8/7 16:26 15 | */ 16 | public class Test { 17 | 18 | public static void main(String[] args) { 19 | CountDownLatch start = new CountDownLatch(1); 20 | CountDownLatch end = new CountDownLatch(100); 21 | 22 | CustomThreadPoolExecutor customThreadPoolExecutor = new CustomThreadPoolExecutor( 23 | 100, 100, 0L, 24 | TimeUnit.SECONDS, 25 | new ArrayBlockingQueue(100) 26 | 27 | ); 28 | 29 | for (int i = 0; i < 100; i++) { 30 | final int finalName = i; 31 | CustomThreadPoolExecutor.CustomTask task = new CustomThreadPoolExecutor.CustomTask( 32 | new Runnable() { 33 | @Override 34 | public void run() { 35 | try { 36 | start.await(); 37 | HttpUtil.get("http://localhost:8081/test/2?name=" + finalName); 38 | } catch (Exception ex) { 39 | ex.printStackTrace(); 40 | } finally { 41 | end.countDown(); 42 | } 43 | } 44 | } 45 | , "success"); 46 | customThreadPoolExecutor.submit(task); 47 | } 48 | 49 | start.countDown(); 50 | try { 51 | end.await(); 52 | } catch (InterruptedException ex) { 53 | ex.printStackTrace(); 54 | } 55 | customThreadPoolExecutor.shutdown(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/controller/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.controller.v2; 2 | 3 | import com.cmazxiaoma.concurrent.CustomThreadPoolExecutor; 4 | import com.cmazxiaoma.controller.HttpUtil; 5 | 6 | import java.util.concurrent.ArrayBlockingQueue; 7 | import java.util.concurrent.CountDownLatch; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | /** 11 | * @author cmazxiaoma 12 | * @version V1.0 13 | * @Description: TODO 14 | * @date 2018/8/7 16:31 15 | */ 16 | public class Test { 17 | 18 | public static void main(String[] args) { 19 | CountDownLatch start = new CountDownLatch(1); 20 | CountDownLatch end = new CountDownLatch(100); 21 | 22 | CustomThreadPoolExecutor customThreadPoolExecutor = new CustomThreadPoolExecutor( 23 | 100, 100, 0L, 24 | TimeUnit.SECONDS, 25 | new ArrayBlockingQueue(100) 26 | 27 | ); 28 | 29 | for (int i = 0; i < 100; i++) { 30 | final int finalName = i; 31 | CustomThreadPoolExecutor.CustomTask task = new CustomThreadPoolExecutor.CustomTask( 32 | new Runnable() { 33 | @Override 34 | public void run() { 35 | try { 36 | start.await(); 37 | HttpUtil.get("http://localhost:8081/test/3?name=" + finalName); 38 | } catch (Exception ex) { 39 | ex.printStackTrace(); 40 | } finally { 41 | end.countDown(); 42 | } 43 | } 44 | } 45 | , "success"); 46 | customThreadPoolExecutor.submit(task); 47 | } 48 | 49 | start.countDown(); 50 | try { 51 | end.await(); 52 | } catch (InterruptedException ex) { 53 | ex.printStackTrace(); 54 | } 55 | customThreadPoolExecutor.shutdown(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/concurrent/SemaphoreTest.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.concurrent; 2 | 3 | import java.util.concurrent.ArrayBlockingQueue; 4 | import java.util.concurrent.Semaphore; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | /** 8 | * @author cmazxiaoma 9 | * @version V1.0 10 | * @Description: TODO 11 | * @date 2018/8/17 17:57 12 | */ 13 | public class SemaphoreTest { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | test1(); 17 | } 18 | 19 | public static void test1() throws InterruptedException { 20 | final Semaphore semaphore = new Semaphore(5); 21 | 22 | CustomThreadPoolExecutor customThreadPoolExecutor = new CustomThreadPoolExecutor( 23 | 10, 10, 0L, 24 | TimeUnit.MILLISECONDS, 25 | new ArrayBlockingQueue(100) 26 | ); 27 | 28 | for (int i = 0; i < 9; i++) { 29 | 30 | CustomThreadPoolExecutor.CustomTask task = 31 | new CustomThreadPoolExecutor.CustomTask(new Runnable() { 32 | @Override 33 | public void run() { 34 | try { 35 | semaphore.acquire(); 36 | System.out.println("thread:" + Thread.currentThread().getName() + "acquire one permit"); 37 | TimeUnit.MILLISECONDS.sleep(500); 38 | System.out.println("thread:" +Thread.currentThread().getName() + "release one permit"); 39 | semaphore.release(); 40 | } catch (InterruptedException ex) { 41 | ex.printStackTrace(); 42 | } 43 | } 44 | }, "success"); 45 | 46 | customThreadPoolExecutor.submit(task); 47 | } 48 | customThreadPoolExecutor.shutdown(); 49 | TimeUnit.SECONDS.sleep(5); 50 | System.out.println(semaphore.availablePermits()); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/netty/demo/oio/PlainOioServer.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.netty.demo.oio; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | import java.net.ServerSocket; 6 | import java.net.Socket; 7 | import java.nio.charset.Charset; 8 | 9 | /** 10 | * @author cmazxiaoma 11 | * @version V1.0 12 | * @Description: TODO 13 | * @date 2018/12/6 17:26 14 | */ 15 | public class PlainOioServer { 16 | 17 | public void serve(int port) throws Exception { 18 | final ServerSocket serverSocket = new ServerSocket(port); 19 | try { 20 | for (;;) { 21 | final Socket clientSocket = serverSocket.accept(); 22 | System.out.println("accepted connection from " + clientSocket); 23 | new Thread(new Runnable() { 24 | @Override 25 | public void run() { 26 | OutputStream out; 27 | try { 28 | out = clientSocket.getOutputStream(); 29 | out.write("Hi\r\n".getBytes(Charset.forName("UTF-8"))); 30 | out.flush(); 31 | clientSocket.close(); 32 | } catch (IOException e) { 33 | e.printStackTrace(); 34 | } finally { 35 | try { 36 | if (clientSocket != null) { 37 | clientSocket.close(); 38 | } 39 | } catch (IOException e) { 40 | e.printStackTrace(); 41 | } 42 | } 43 | } 44 | }).start(); 45 | } 46 | } catch (Exception e) { 47 | 48 | } finally { 49 | // todo 50 | } 51 | } 52 | 53 | public static void main(String[] args) throws Exception { 54 | PlainOioServer plainOioServer = new PlainOioServer(); 55 | plainOioServer.serve(9998); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/cmazxiaoma/alibaba/贪心算法/TestDongtai.java: -------------------------------------------------------------------------------- 1 | package com.cmazxiaoma.alibaba.贪心算法; 2 | 3 | /** 4 | * @author xiaoma 5 | * @version V1.0 6 | * @Description: 动态规划 找硬币问题 7 | * @date 2020/2/14 22:18 8 | */ 9 | 10 | /** 11 | * 给你一根长度为n的绳子,请把绳子剪成m段, 12 | * 请问最终每段绳子长度的乘积最大值是多少?例如,当绳子的长度为8时,我们剪成3,3,2三段,最大乘积是18。 13 | */ 14 | 15 | /** 16 | * 求一个问题的最优解 17 | * 我们把长度为n的绳子剪成若干段后的乘积记为f(n),假设我们第一刀剪在了长度为i的位置(0