This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func main() { | |
| c := make(chan int) // 不是一个名为chan类型为int的参数,而是一个Channel类型: chan int | |
| fmt.Printf("type:%T\n", c) //type:chan int | |
| // 看上去,Channel实际上是一个Pointer? | |
| fmt.Printf("value:%v\n", c) // value:0xc820078060 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func main() { | |
| v1 := 1 | |
| v2 := &v1 | |
| v3 := &v2 | |
| v4 := &v2 | |
| v5 := &v4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func main() { | |
| c := make(chan bool) | |
| m := make(map[string]string) | |
| go func() { | |
| m["1"] = "a" // First conflicting access. | |
| c <- true | |
| }() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.mysql.jdbc.MySQLConnection; | |
| import java.sql.*; | |
| import java.util.concurrent.TimeUnit; | |
| /** | |
| * MySQL 默认可重复读RR. | |
| * 两个线程T1,T2 分别执行一个业务单元. | |
| * T1中先 UPDATE test1 set field1='AA' where id=1, 然后 select field1 from test1 where id=1就应该返回 AA. | |
| * T2同理, set BB 后,select出的也应该是 BB . | |
| * <p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import java.net.InetSocketAddress; | |
| import java.net.Socket; | |
| import java.util.concurrent.TimeUnit; | |
| public class SimultaneousOpenConnectionEstablishment { | |
| private static int port1 = 18081; | |
| private static int port2 = 18082; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.WeakHashMap; | |
| import java.util.concurrent.TimeUnit; | |
| /** | |
| * 展示会引起 MemoryLeak 的 WeakHashMap 错误用法: value 引用了 key. | |
| * <ul> | |
| * <li> | |
| * 1、 java.util.WeakHashMap 的 key (String类型) 在没有 strongly reachable 时,就会被自动回收。 | |
| * <li> | |
| * 2、 key、value会组成一个 WeakReference 的子类 Entry(继承不是WeakReference的必备),但 referent object 仅仅包括 key. 所以: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.concurrent.*; | |
| public class QueuesTaskExecuteLaterDemo { | |
| public static void main(String[] args) throws InterruptedException { | |
| ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(90); | |
| ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, | |
| 5, TimeUnit.SECONDS, workQueue); | |
| executor.allowCoreThreadTimeOut(true); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.concurrent.*; | |
| public class AddWorkerDemo { | |
| public static void main(String[] args) throws InterruptedException { | |
| ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(190); | |
| ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, | |
| 190, TimeUnit.SECONDS, workQueue); | |
| Runnable r = new Runnable() { | |
| @Override |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 2018-12-11 17:42:41 | |
| Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.111-b14 mixed mode): | |
| "HikariPool-1 connection closer" #26929 daemon prio=5 os_prio=0 tid=0x00007f06f8b06000 nid=0x6940 waiting on condition [0x00007f06d2e2e000] | |
| java.lang.Thread.State: TIMED_WAITING (parking) | |
| at sun.misc.Unsafe.park(Native Method) | |
| - parking to wait for <0x00000000c470e670> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) | |
| at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215) | |
| at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078) | |
| at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainObjectLong { | |
| public static void main(String[] args) { | |
| try { | |
| System.out.println(Object.test42()); | |
| System.out.println(Long.test43()); | |
| System.out.println("success:加载的是自定义的 Object/Long"); | |
| } catch (Exception ex) { | |
| System.out.println("exception:"); |