Permalink
Browse files

a little changes, a few lines of code to exam java lang

  • Loading branch information...
1 parent c4a48bd commit adb479b73af5dbbfffcc96aa3156bb54555cfe01 feng committed Mar 5, 2011
View
@@ -37,7 +37,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
- <version>r07</version>
+ <version>r08</version>
</dependency>
</dependencies>
</project>
@@ -3,11 +3,24 @@
<log4j:configuration>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %5p [%t] %c{1} - %m%n" />
+ <param name="ConversionPattern" value="%d{HH:mm:ss} [%t]-%m%n" />
+ <!--<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %5p [%t] %c{1}
+ - %m%n" /> -->
</layout>
</appender>
+
+ <appender name="ROLL" class="org.apache.log4j.RollingFileAppender">
+ <param name="File" value="/tmp/download.log" />
+ <param name="Append" value="true" />
+ <param name="MaxBackupIndex" value="10" />
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%d{HH:mm:ss} [%t]-%m%n" />
+ </layout>
+ </appender>
+
<root>
<priority value="debug"></priority>
<appender-ref ref="stdout" />
+ <appender-ref ref="ROLL" />
</root>
</log4j:configuration>
@@ -0,0 +1,57 @@
+package jcip;
+
+import java.util.Random;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Test;
+
+import com.google.common.collect.MapMaker;
+
+public class CacheTest {
+
+ private static final Random r = new Random();
+
+ private static class MyKey {
+ private int[] value = new int[r.nextInt(1000)];
+
+ public MyKey() {
+ for (int i = 0; i < value.length; i++) {
+ value[i] = i;
+ }
+ }
+ }
+
+ private static class MyValue {
+ private Object[] value = new Object[r.nextInt(10000)];
+
+ public MyValue() {
+ for (int i = 0; i < value.length; i++) {
+ value[i] = i;
+ }
+ }
+ }
+
+ @Test
+ public void testCache() {
+
+ ConcurrentMap<Object, Object> map = new MapMaker().softKeys()
+ .softValues().makeMap();
+ for (int i = 0; i < 10; i++) {
+ map.put(1, 1);
+
+ System.out.println(map);
+ }
+
+//
+// for (;;) {
+// map.put(new MyKey(), new MyValue());
+//// map.get
+// try {
+// TimeUnit.MILLISECONDS.sleep(r.nextInt(10));
+// } catch (InterruptedException ignore) {
+// }
+// }
+
+ }
+}
@@ -0,0 +1,64 @@
+package jcip;
+
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Test;
+
+public class DeadLockTest {
+
+ private static class TestRunnale implements Runnable {
+
+ private Object lock1;
+ private Object lock2;
+ private Random r = new Random();
+
+ public TestRunnale(Object lock1, Object lock2) {
+ this.lock1 = lock1;
+ this.lock2 = lock2;
+ }
+
+ @Override
+ public void run() {
+ while (true) {
+ synchronized (lock1) {
+ try {
+ TimeUnit.MILLISECONDS.sleep(r.nextInt(100));
+ System.out.println(Thread.currentThread().getName()
+ + " success");
+ } catch (InterruptedException ingore) {
+ }
+ synchronized (lock2) {
+ try {
+ TimeUnit.MILLISECONDS.sleep(r.nextInt(100));
+ System.out.println(Thread.currentThread().getName()
+ + " success");
+ } catch (InterruptedException ingore) {
+ }
+ }
+ }
+ }
+ }
+
+ }
+
+ // this sure will deak lock. kill -3 {pid} to thread dump, or
+ // Ctrl+\ on linux and Ctrl+Break on windows
+ public static void main(String[] args) throws InterruptedException {
+ new DeadLockTest().testDeadLock();
+ }
+
+ @Test
+ public void testDeadLock() throws InterruptedException {
+ final Object lock1 = new Object();
+ final Object lock2 = new Object();
+ Thread t1 = new Thread(new TestRunnale(lock1, lock2));
+ Thread t2 = new Thread(new TestRunnale(lock2, lock1));
+
+ t1.start();
+ t2.start();
+
+ t1.join();
+ t2.join();
+ }
+}
@@ -5,34 +5,37 @@
public class JavaLangLearn {
- private static class TestClone implements Cloneable {
- public TestClone(int j) {
- i = j;
- }
-
- // @Override
- // protected TestClone clone() throws CloneNotSupportedException {
- // return (TestClone) super.clone();
- // }
-
- public int i;
- }
-
- @Test
- public void testArrayCopy() throws CloneNotSupportedException {
- Integer iarr[] = new Integer[] { 1, 2, 3, 4, 5 };
- Integer[] cloned = iarr.clone();
- iarr[0] = 10;
- Assert.assertEquals(1, cloned[0].intValue());
-
- TestClone tc = new TestClone(10);
- // TestClone c2 = tc.clone();
- // Assert.assertEquals(10, c2.i);
-
- TestClone tcArr[] = new TestClone[] { tc };
- TestClone[] c3 = tcArr.clone();
- tc.i = 100;
- // shadow copy
- Assert.assertEquals(100, c3[0].i);
- }
+ private static class TestClone implements Cloneable {
+ public TestClone(int j) {
+ i = j;
+ }
+
+ public int i;
+ }
+
+ @Test
+ public void testArrayCopy() throws CloneNotSupportedException {
+ Integer iarr[] = new Integer[] { 1, 2, 3, 4, 5 };
+ Integer[] cloned = iarr.clone();
+ iarr[0] = 10;
+ // primitive copy are not shadow
+ Assert.assertEquals(1, cloned[0].intValue());
+
+ TestClone tc = new TestClone(10);
+ TestClone tcArr[] = new TestClone[] { tc };
+ TestClone[] copyed = tcArr.clone();
+ tc.i = 100;
+ // shadow copy
+ Assert.assertEquals(100, copyed[0].i);
+ }
+
+ @Test
+ public void testEqual() {
+ Integer i = 11111;
+ Integer j = 11111;
+ // == && != is broken
+ Assert.assertFalse(i == j);
+
+ //System.out.println();
+ }
}

0 comments on commit adb479b

Please sign in to comment.