OPTIONS: -A, --after-context <value> Print NUM lines of trailing context) -B, --before-context <value> Print NUM lines of leading context) -C, --context <value> Print NUM lines of output context) -h, --help this help -i, --ignore-case Perform case insensitive matching. By default, grep is case sensitive. -v, --invert-match Select non-matching lines -n, --line-number Print line number with output lines -m, --max-count <value> stop after NUM selected lines) -e, --regex Enable regular expression to match --trim-end Remove whitespaces at the end of the line <pattern> Pattern
$ getstatic demo.MathGame random field: random @Random[ serialVersionUID=@Long[3905348978240129619], seed=@AtomicLong[120955813885284], multiplier=@Long[25214903917], addend=@Long[11], mask=@Long[281474976710655], DOUBLE_UNIT=@Double[1.1102230246251565E-16], BadBound=@String[bound must be positive], BadRange=@String[bound must be greater than origin], BadSize=@String[size must be non-negative], seedUniquifier=@AtomicLong[-3282039941672302964], nextNextGaussian=@Double[0.0], haveNextNextGaussian=@Boolean[false], serialPersistentFields=@ObjectStreamField[][isEmpty=false;size=3], unsafe=@Unsafe[sun.misc.Unsafe@2eaa1027], seedOffset=@Long[24], ]
OPTIONS: -h, --help this help -i, --interval <value> The interval (in ms) between two executions, default is 5000 ms. -n, --number-of-execution <value> The number of times this command will be executed.
history 命令
打印命令历史。
提示
历史指令会通过一个名叫 history 的文件持久化,所以 history 指令可以查看当前 arthas 服务器的所有历史命令,而不仅只是当前次会话使用过的命令。
参数说明
参数名称
参数说明
[c:]
清空历史指令
[n:]
显示最近执行的 n 条指令
使用参考
1 2 3 4 5
#查看最近执行的3条指令 $ history 3 269 thread 270 cls 271 history 3
Metaspace Manager Metaspace Compressed Class Space
Copy Eden Space Survivor Space
MarkSweepCompact Eden Space Survivor Space Tenured Gen
-------------------------------------------------------------------------------------------------------------- MEMORY -------------------------------------------------------------------------------------------------------------- HEAP-MEMORY-USAGE init : 268435456(256.0 MiB) [memory in bytes] used : 18039504(17.2 MiB) committed : 181403648(173.0 MiB) max : 3817865216(3.6 GiB)
NO-HEAP-MEMORY-USAGE init : 2555904(2.4 MiB) [memory in bytes] used : 33926216(32.4 MiB) committed : 35176448(33.5 MiB) max : -1(-1 B)
-------------------------------------------------------------------------------------------------------------- OPERATING-SYSTEM -------------------------------------------------------------------------------------------------------------- OS Linux ARCH amd64 PROCESSORS-COUNT 3 LOAD-AVERAGE 29.53 VERSION 4.15.0-52-generic
[arthas@2062]$ logger name ROOT class ch.qos.logback.classic.Logger classLoader sun.misc.Launcher$AppClassLoader@2a139a55 classLoaderHash 2a139a55 level INFO effectiveLevel INFO additivity true codeSource file:/Users/hengyunabc/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar appenders name CONSOLE class ch.qos.logback.core.ConsoleAppender classLoader sun.misc.Launcher$AppClassLoader@2a139a55 classLoaderHash 2a139a55 target System.out name APPLICATION class ch.qos.logback.core.rolling.RollingFileAppender classLoader sun.misc.Launcher$AppClassLoader@2a139a55 classLoaderHash 2a139a55 file app.log name ASYNC class ch.qos.logback.classic.AsyncAppender classLoader sun.misc.Launcher$AppClassLoader@2a139a55 classLoaderHash 2a139a55 appenderRef [APPLICATION]
$ ognl '@demo.MathGame@random' @Random[ serialVersionUID=@Long[3905348978240129619], seed=@AtomicLong[125451474443703], multiplier=@Long[25214903917], addend=@Long[11], mask=@Long[281474976710655], DOUBLE_UNIT=@Double[1.1102230246251565E-16], BadBound=@String[bound must be positive], BadRange=@String[bound must be greater than origin], BadSize=@String[size must be non-negative], seedUniquifier=@AtomicLong[-3282039941672302964], nextNextGaussian=@Double[0.0], haveNextNextGaussian=@Boolean[false], serialPersistentFields=@ObjectStreamField[][isEmpty=false;size=3], unsafe=@Unsafe[sun.misc.Unsafe@28ea5898], seedOffset=@Long[24], ]
OPTIONS: -h, --help this help -E, --regex Enable regular expression to match (wildcard matching by default) <class-pattern> Path and classname of Pattern Matching
还原指定类
1 2 3 4 5 6 7 8 9 10 11
$ trace Test test Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 57 ms. `---ts=2017-10-26 17:10:33;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@14dad5dc `---[0.590102ms] Test:test()
$ reset Test Affect(class-cnt:1 , method-cnt:0) cost in 11 ms.
还原所有类
1 2 3 4 5 6 7 8
$ trace Test test Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 15 ms. `---ts=2017-10-26 17:12:06;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@14dad5dc `---[0.128518ms] Test:test()
$ reset Affect(class-cnt:1 , method-cnt:0) cost in 9 ms.
$ stack demo.MathGame primeFactors Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 36 ms. ts=2018-12-04 01:32:19;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@3d4eac69 @demo.MathGame.run() at demo.MathGame.main(MathGame.java:16)
指定 Class 最大匹配数量
1 2 3 4 5 6 7
$ stack demo.MathGame primeFactors -m 1 Press Q or Ctrl+C to abort. Affect(class count:1 , method count:1) cost in 561 ms, listenerId: 5. ts=2022-12-25 21:07:07;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@b4aac2 @demo.MathGame.primeFactors() at demo.MathGame.run(MathGame.java:46) at demo.MathGame.main(MathGame.java:38)
据条件表达式来过滤
1 2 3 4 5 6 7 8 9 10 11 12
$ stack demo.MathGame primeFactors 'params[0]<0' -n 2 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 30 ms. ts=2018-12-04 01:34:27;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@3d4eac69 @demo.MathGame.run() at demo.MathGame.main(MathGame.java:16)
ts=2018-12-04 01:34:30;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@3d4eac69 @demo.MathGame.run() at demo.MathGame.main(MathGame.java:16)
Command execution times exceed limit: 2, so command will exit. You can set it with -n option.
据执行时间来过滤
1 2 3 4 5 6
$ stack demo.MathGame primeFactors '#cost>5' Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 35 ms. ts=2018-12-04 01:35:58;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@3d4eac69 @demo.MathGame.run() at demo.MathGame.main(MathGame.java:16)
对于具体一个时间片的信息而言,你可以通过 -i 参数后边跟着对应的 INDEX 编号查看到他的详细信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ tt -i 1003 INDEX 1003 GMT-CREATE 2018-12-04 11:15:41 COST(ms) 0.186073 OBJECT 0x4b67cf4d CLASS demo.MathGame METHOD primeFactors IS-RETURN false IS-EXCEPTION true PARAMETERS[0] @Integer[-564322413] THROW-EXCEPTION java.lang.IllegalArgumentException: number is: -564322413, need >= 2 at demo.MathGame.primeFactors(MathGame.java:46) at demo.MathGame.run(MathGame.java:24) at demo.MathGame.main(MathGame.java:16)
Metaspace Manager Metaspace Compressed Class Space
Copy Eden Space Survivor Space
MarkSweepCompact Eden Space Survivor Space Tenured Gen
-------------------------------------------------------------------------------------------------------------- MEMORY -------------------------------------------------------------------------------------------------------------- HEAP-MEMORY-USAGE init : 268435456(256.0 MiB) [memory in bytes] used : 18039504(17.2 MiB) committed : 181403648(173.0 MiB) max : 3817865216(3.6 GiB)
NO-HEAP-MEMORY-USAGE init : 2555904(2.4 MiB) [memory in bytes] used : 33926216(32.4 MiB) committed : 35176448(33.5 MiB) max : -1(-1 B)
-------------------------------------------------------------------------------------------------------------- OPERATING-SYSTEM -------------------------------------------------------------------------------------------------------------- OS Linux ARCH amd64 PROCESSORS-COUNT 3 LOAD-AVERAGE 29.53 VERSION 4.15.0-52-generic
$ options LEVEL TYPE NAME VALUE SUMMARY DESCRIPTION ------------------------------------------------------------------------------------------------------- 0 boolea unsafe false Option to support sy This option enables to proxy functionality n stem-level class of JVM classes. Due to serious security r isk a JVM crash is possibly be introduced. Do not activate it unless you are able to manage. 1 boolea dump false Option to dump the e This option enables the enhanced classes t n nhanced classes o be dumped to external file for further d e-compilation and analysis. 1 boolea batch-re-tra true Option to support ba This options enables to reTransform classe n nsform tch reTransform Clas s with batch mode. s 2 boolea json-format false Option to support JS This option enables to format object outpu n ON format of object t with JSON when -x option selected. output 1 boolea disable-sub- false Option to control in This option disable to include sub class w n class clude sub class when hen matching class. class matching 1 boolea support-defa true Option to control in This option disable to include default met n ult-method clude default method hod in interface when matching class. in interface when c lass matching 1 boolea save-result false Option to print comm This option enables to save each command's n and's result to log result to log file, which path is ${user. file home}/logs/arthas-cache/result.log. 2 String job-timeout 1d Option to job timeou This option setting job timeout,The unit c t an be d, h, m, s for day, hour, minute, se cond. 1d is one day in default 1 boolea print-parent true Option to print all This option enables print files in parent n -fields fileds in parent cla class, default value true. ss 1 boolea verbose false Option to print verb This option enables print verbose informat n ose information ion, default value false. 1 boolea strict true Option to strict mod By default, strict mode is true, not allow n e ed to set object properties. Want to set o bject properties, execute `options strict false`
参数说明
名称
默认值
描述
unsafe
false
是否支持对系统级别的类进行增强,打开该开关可能导致把 JVM 搞挂,请慎重选择!
dump
false
是否支持被增强了的类 dump 到外部文件中,如果打开开关,class 文件会被 dump 到/${application working dir}/arthas-class-dump/目录下,具体位置详见控制台输出
$ options json-format LEVEL TYPE NAME VALUE SUMMARY DESCRIPTION -------------------------------------------------------------------------------------------- 2 bool json-format false Option to support This option enables to format object ean JSON format of obj output with JSON when -x option selec ect output ted.
[arthas@6392]$ watch com.example.demo.arthas.user.UserController * '{params, throwExp}' -x 2 Press Q or Ctrl+C to abort. Affect(class count: 1 , method count: 2) cost in 119 ms, listenerId: 1 method=com.example.demo.arthas.user.UserController.findUserById location=AtExceptionExit ts=2024-01-05 16:38:32; [cost=6.8937ms] result=@ArrayList[ @Object[][ @Integer[0], ], java.lang.IllegalArgumentException: id < 1 at com.example.demo.arthas.user.UserController.findUserById(UserController.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ...... ]
[arthas@3880]$ stack javax.servlet.http.HttpServletResponse sendError 'params[0]==401' Press Q or Ctrl+C to abort. Affect(class count: 3 , method count: 4) cost in 99 ms, listenerId: 4 ts=2024-01-06 16:33:24;thread_name=http-nio-80-exec-3;id=14;is_daemon=true;priority=5;TCCL=org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedWebappClassLoader@54c14576 @org.apache.catalina.connector.Response.sendError() at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:462) at com.example.demo.arthas.AdminFilterConfig$AdminFilter.doFilter(AdminFilterConfig.java:38) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)
$ help NAME DESCRIPTION help Display Arthas Help auth Authenticates the current session keymap Display all the available keymap for the specified connection. sc Search all the classes loaded by JVM sm Search the method of classes loaded by JVM classloader Show classloader info jad Decompile class getstatic Show the static field of a class monitor Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc. stack Display the stack trace for the specified class and method thread Display thread info, thread stack trace Trace the execution time of specified method invocation. watch Display the input/output parameter, return object, and thrown exception of specified method invocation tt Time Tunnel jvm Display the target JVM information perfcounter Display the perf counter information. ognl Execute ognl expression. mc Memory compiler, compiles java files into bytecode and class files in memory. redefine Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...) retransform Retransform classes. @see Instrumentation#retransformClasses(Class...) dashboard Overview of target jvm's thread, memory, gc, vm, tomcat info. dump Dump class byte array from JVM heapdump Heap dump options View and change various Arthas options cls Clear the screen reset Reset all the enhanced classes version Display Arthas version session Display current session information sysprop Display, and change the system properties. sysenv Display the system env. vmoption Display, and update the vm diagnostic options. logger Print logger info, and update the logger level history Display command history cat Concatenate and print files base64 Encode and decode using Base64 representation echo write arguments to the standard output pwd Return working directory name mbean Display the mbean information grep grep command for pipes. tee tee command for pipes. profiler Async Profiler. https://github.com/jvm-profiling-tools/async-profiler stop Stop/Shutdown Arthas server and exit the console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$ help dashboard USAGE: dashboard [-h] [-i <value>] [-n <value>]
OPTIONS: -h, --help this help -i, --interval <value> The interval (in ms) between two executions, default is 5000 ms. -n, --number-of-execution <value> The number of times this command will be executed.
Thread
thread 1 命令会打印线程 ID 1 的栈。
Arthas 支持管道,可以用 thread 1 | grep 'main(' 查找到main class 。
可以看到main class 是demo.MathGame :
1 2
$ thread 1 | grep 'main(' at demo.MathGame.main(MathGame.java:17)
提示
查看当前线程信息,查看线程的堆栈。
参数说明
参数名称
参数说明
id
线程 id
[n:]
指定最忙的前 N 个线程并打印堆栈
[b]
找出当前阻塞其他线程的线程
[i <value>]
指定 cpu 使用率统计的采样间隔,单位为毫秒,默认值为 200
[–all]
显示所有匹配的线程
使用参考
支持一键展示当前最忙的前 N 个线程并打印堆栈:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
"Reference Handler" Id=2 cpuUsage=0.0% deltaTime=0ms time=0ms WAITING on java.lang.ref.Reference$Lock@2718d45c at java.lang.Object.wait(Native Method) - waiting on java.lang.ref.Reference$Lock@2718d45c at java.lang.Object.wait(Unknown Source) at java.lang.ref.Reference.tryHandlePending(Unknown Source) at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
"Finalizer" Id=3 cpuUsage=0.0% deltaTime=0ms time=0ms WAITING on java.lang.ref.ReferenceQueue$Lock@6ce75d0d at java.lang.Object.wait(Native Method) - waiting on java.lang.ref.ReferenceQueue$Lock@6ce75d0d at java.lang.ref.ReferenceQueue.remove(Unknown Source) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)
$ thread 1 "main" Id=1 TIMED_WAITING at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Unknown Source) at java.util.concurrent.TimeUnit.sleep(Unknown Source) at demo.MathGame.main(MathGame.java:17)
$ thread -b "http-bio-8080-exec-4" Id=27 TIMED_WAITING at java.lang.Thread.sleep(Native Method) at test.arthas.TestThreadBlocking.doGet(TestThreadBlocking.java:22) - locked java.lang.Object@725be470 <---- but blocks 4 other threads! at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at test.filter.TestDurexFilter.doFilter(TestDurexFilter.java:46) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at com.taobao.tomcat.valves.ContextLoadFilterValve$FilterChainAdapter.doFilter(ContextLoadFilterValve.java:191) at com.taobao.eagleeye.EagleEyeFilter.doFilter(EagleEyeFilter.java:81) at com.taobao.tomcat.valves.ContextLoadFilterValve.invoke(ContextLoadFilterValve.java:150) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:429) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) - locked org.apache.tomcat.util.net.SocketWrapper@7127ee12 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
Number of locked synchronizers = 1 - java.util.concurrent.ThreadPoolExecutor$Worker@31a6493e
switch (weatherType) { case SUNNY: System.out.println("天气晴朗,动物们出去觅食了!"); break; case RAINY: System.out.println("下雨了,动物们找地方避雨了!"); break; case WINDY: System.out.println("起风了,动物们在睡大觉!"); break; case COLD: System.out.println("天冷了,动物们抱团取暖!"); } }
publicstaticvoidmain(String[] args){ // 生产城市汽车 CityCarBuilder cityCarBuilder = new CityCarBuilder(); Director director = new Director(cityCarBuilder); Car cityCar = director.constructCar(); System.out.println(cityCar.toString());
// 生产运动汽车 SportCarBuilder sportCarBuilder = new SportCarBuilder(); director.setBuilder(sportCarBuilder); Car sportCar = director.constructCar(); System.out.println(sportCar.toString());
// 生产SUV SUVCarBuilder suvCarBuilder = new SUVCarBuilder(); director.setBuilder(suvCarBuilder); Car suv = director.constructCar(); System.out.println(suv.toString());
Random random = new Random(); Supplier<Integer> integerSupplier = () -> random.nextInt(100); System.out.println(integerSupplier.get()); System.out.println(integerSupplier.get()); }
Out:
1 2 3
hello world 65 54
使用方法引用实例化Supplier
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicstaticvoidmain(String[] args){ Supplier<Person> personSupplier = Person::new; Person person1 = personSupplier.get(); person1.setName("张三"); Person person2 = personSupplier.get(); person2.setName("李四"); System.out.println(person1); System.out.println(person2);
Random random = new Random(); Supplier<Integer> randomSupplier = random::nextInt; System.out.println(randomSupplier.get()); System.out.println(randomSupplier.get()); }