diff --git a/src/Chapter21/Concurrency/DeamonZ.java b/src/Chapter21/Concurrency/DeamonZ.java new file mode 100644 index 0000000..439fe5d --- /dev/null +++ b/src/Chapter21/Concurrency/DeamonZ.java @@ -0,0 +1,25 @@ +package Chapter21.Concurrency; + +public class DeamonZ { + + public static void main(String[] args) { + Thread thread = new Thread(new DeamonRunner(),"liershuang");System.out.println("----"); + thread.setDaemon(true); + thread.start(); + } + static class DeamonRunner implements Runnable{ + + @Override + public void run() { + try { + Thread.sleep(100000L); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }finally { + System.out.println("DeamonThread finally run."); + } + } + + } +} diff --git a/src/Chapter21/Concurrency/DelayQueueTest.java b/src/Chapter21/Concurrency/DelayQueueTest.java new file mode 100644 index 0000000..e4f4cbd --- /dev/null +++ b/src/Chapter21/Concurrency/DelayQueueTest.java @@ -0,0 +1,66 @@ +package Chapter21.Concurrency; + +import java.util.Date; +import java.util.concurrent.AbstractExecutorService; +import java.util.concurrent.DelayQueue; +import java.util.concurrent.Delayed; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +public class DelayQueueTest { + static DelayQueue dq = new DelayQueue<>(); + public static void main(String[] args) throws InterruptedException { + MyClass mc1 = new MyClass(1); + + dq.put(mc1); + dq.put(new MyClass(5)); + dq.put(new MyClass(2)); + System.out.println(dq); + dq.take(); + System.out.println(new Date()); + dq.take(); + System.out.println(new Date()); + dq.take(); + System.out.println(new Date()); + + } + + static class MyClass implements Delayed{ + public int number; + + public int getNumber() { + return number; + } + + public void setNumber(int number) { + this.number = number; + } + + public MyClass(int number) { + super(); + this.number = number; + } + + @Override + public int compareTo(Delayed o) { + MyClass mc = null; + if(o instanceof MyClass) { + mc = (MyClass)o; + }else { + return 0; + } + return this.getNumber() > mc.getNumber()?1:(this.getNumber() < mc.getNumber()?-1:0); + } + + @Override + public long getDelay(TimeUnit unit) { + return 5; + } + + @Override + public String toString() { + return "MyClass [number=" + number + "]"; + } + + } +} diff --git a/src/Chapter21/Concurrency/Interrupted.java b/src/Chapter21/Concurrency/Interrupted.java new file mode 100644 index 0000000..6ed9336 --- /dev/null +++ b/src/Chapter21/Concurrency/Interrupted.java @@ -0,0 +1,46 @@ +package Chapter21.Concurrency; + +public class Interrupted { + + public static void main(String[] args) throws InterruptedException { + Thread sleepT = new Thread(new SleepRunner(),"SleepThread"); + sleepT.setDaemon(true); + Thread busyT = new Thread(new BusyRunner(),"BusyThread"); + busyT.setDaemon(true); + sleepT.start(); + busyT.start(); + Thread.sleep(5000L); + sleepT.interrupt(); + busyT.interrupt(); + busyT.interrupted(); + busyT.interrupted(); + Thread.sleep(5000L); + System.out.println("sleepThread interrupted is "+sleepT.isInterrupted()); + System.out.println("busyThread interrupted is "+busyT.isInterrupted()); + Thread.sleep(10000L); + } + + static class SleepRunner implements Runnable{ + + @Override + public void run() { + while(true) { + try { + Thread.sleep(1000L); + } catch (InterruptedException e) { + System.out.println("中断了哪"); + } + } + } + + } + + static class BusyRunner implements Runnable{ + + @Override + public void run() { + while(true) {} + } + + } +} diff --git a/src/Chapter21/Concurrency/MultiThread.java b/src/Chapter21/Concurrency/MultiThread.java new file mode 100644 index 0000000..ee4cb58 --- /dev/null +++ b/src/Chapter21/Concurrency/MultiThread.java @@ -0,0 +1,17 @@ +package Chapter21.Concurrency; + +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadInfo; +import java.lang.management.ThreadMXBean; + +public class MultiThread { + + public static void main(String[] args) { + ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); + ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false); + for(ThreadInfo threadInfo:threadInfos) { + System.out.println("["+threadInfo.getThreadId()+"]"+threadInfo.getThreadName()); + } + } + +} diff --git a/src/Chapter21/Concurrency/SynchronousQueueTest.java b/src/Chapter21/Concurrency/SynchronousQueueTest.java new file mode 100644 index 0000000..95ca9cd --- /dev/null +++ b/src/Chapter21/Concurrency/SynchronousQueueTest.java @@ -0,0 +1,46 @@ +package Chapter21.Concurrency; + +import java.util.concurrent.SynchronousQueue; + +public class SynchronousQueueTest { + static SynchronousQueue sq = new SynchronousQueue<>(); + + public static void main(String[] args) throws InterruptedException { + TakeT takeT = new TakeT(); + PutT putT = new PutT(); + Thread t1 = new Thread(takeT); + Thread p1 = new Thread(putT); + + p1.start(); + Thread.sleep(10000L); + t1.start(); + Thread.sleep(5000L); + System.out.println(sq); + } + + static class TakeT implements Runnable{ + @Override + public void run() { + try { + System.out.println(sq.take()); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + static class PutT implements Runnable{ + + @Override + public void run() { + try { + sq.put("2018-01-16"); + System.out.println("放入一个元素"); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } +} diff --git a/src/Chapter21/Concurrency/TwinsLock.java b/src/Chapter21/Concurrency/TwinsLock.java new file mode 100644 index 0000000..8327597 --- /dev/null +++ b/src/Chapter21/Concurrency/TwinsLock.java @@ -0,0 +1,73 @@ +package Chapter21.Concurrency; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.AbstractQueuedSynchronizer; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; + +public class TwinsLock implements Lock { + private final Sync sync = new Sync(2); + + private static final class Sync extends AbstractQueuedSynchronizer{ + Sync(int count){ + if(count <= 0) { + throw new IllegalArgumentException("count must large than zero."); + } + setState(count); + } + @Override + public int tryAcquireShared(int reduceCount) { + for(;;) { + int current = getState(); + int newCount = current - reduceCount; + if(newCount <= 0 || compareAndSetState(current,newCount)) { + return newCount; + } + } + } + @Override + public boolean tryReleaseShared(int returnCount) { + for(;;) { + int current = getState(); + int newCount = current + returnCount; + if(compareAndSetState(current,newCount)) { + return true; + } + } + } + } + @Override + public void lock() { + sync.acquireShared(1); + } + + @Override + public void lockInterruptibly() throws InterruptedException { + // TODO Auto-generated method stub + + } + + @Override + public boolean tryLock() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { + // TODO Auto-generated method stub + return false; + } + + @Override + public void unlock() { + sync.releaseShared(1); + } + + @Override + public Condition newCondition() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/Chapter21/Concurrency/TwinsLockTest.java b/src/Chapter21/Concurrency/TwinsLockTest.java new file mode 100644 index 0000000..8d9423a --- /dev/null +++ b/src/Chapter21/Concurrency/TwinsLockTest.java @@ -0,0 +1,39 @@ +package Chapter21.Concurrency; + +import java.util.concurrent.locks.Lock; + +public class TwinsLockTest { + + public static void main(String[] args) throws InterruptedException { + final Lock lock = new TwinsLock(); + class Worker extends Thread{ + public void run() { + while(true) { + lock.lock(); + try { + try { + Thread.sleep(1000L); + System.out.println(Thread.currentThread().getName()); + Thread.sleep(1000L); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + }finally { + lock.unlock(); + } + } + } + } + for(int i=0;i<10;i++){ + Worker w = new Worker(); + w.setDaemon(true); + w.start(); + } + for(int i=0;i<10;i++) { + Thread.sleep(1000L); + System.out.println(); + } + } + +} diff --git a/src/Other/InstanceOf.java b/src/Other/InstanceOf.java new file mode 100644 index 0000000..da546f5 --- /dev/null +++ b/src/Other/InstanceOf.java @@ -0,0 +1,58 @@ +package Other; + +import java.util.concurrent.Callable; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.RunnableFuture; +import java.util.concurrent.ThreadPoolExecutor; + +public class InstanceOf implements Cloneable{ + private int num = 0; + public boolean isTrue(){ + return this.clone().equals(this); + } + public static void main(String[] args) throws CloneNotSupportedException { + InstanceOf io = new InstanceOf(); + System.out.println(io.isTrue()); + + } + @Override + public InstanceOf clone() { + return new InstanceOf(); + } + @Override + public boolean equals(Object o) { + return this.getNum() == ((InstanceOf)o).getNum(); + } + public int getNum() { + return num; + } + public void setNum(int num) { + this.num = num; + } + +} + +class Foo{ + public Foo create() { + return new Foo(); + } +} +class Bar extends Foo{ + @Override + public Bar create() { + return new Bar(); + } +} +class Baz extends Bar{ + @Override + public Bar create() { + return new Baz(); + } +} + + + + + diff --git a/src/btp/oneP/ABtpTest.java b/src/btp/oneP/ABtpTest.java index efdc18c..ce51024 100644 --- a/src/btp/oneP/ABtpTest.java +++ b/src/btp/oneP/ABtpTest.java @@ -6,6 +6,12 @@ import java.util.LinkedList; import java.util.List; import java.util.Scanner; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.AbstractQueuedSynchronizer; +import java.util.concurrent.locks.ReentrantLock; import java.util.zip.DeflaterOutputStream; public class ABtpTest { @@ -31,6 +37,13 @@ public static void main(String[] args) { sc.close(); p.close(); } + + AbstractQueuedSynchronizer aqs = null; + CountDownLatch cdl = null; + Semaphore sp = null; + ArrayBlockingQueue bq = null; + ReentrantLock lock = null; + AtomicInteger ai = null; } public static ArrayList returnArray(){ diff --git a/src/btp/oneP/AQSTest.java b/src/btp/oneP/AQSTest.java new file mode 100644 index 0000000..3e02d70 --- /dev/null +++ b/src/btp/oneP/AQSTest.java @@ -0,0 +1,23 @@ +package btp.oneP; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.AbstractQueuedSynchronizer; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +public class AQSTest { + + public static void main(String[] args) { + AbstractQueuedSynchronizer aqs = null; + AtomicInteger ai = null; + Semaphore sp = null; + CountDownLatch cd = null; + ReentrantLock lock = null; + ReadWriteLock rwl = null; + ReentrantReadWriteLock rwls = null; + } + +} diff --git a/src/btp/oneP/DirectMemoryOOM.java b/src/btp/oneP/DirectMemoryOOM.java new file mode 100644 index 0000000..862fe25 --- /dev/null +++ b/src/btp/oneP/DirectMemoryOOM.java @@ -0,0 +1,12 @@ +package btp.oneP; + +import java.lang.reflect.Field; + +public class DirectMemoryOOM { + private static final int _1MB = 1024*1024; + + public static void main(String[] args) { + //Field unsafeField = Unsafe.class.getDeclaredFields()[0]; + } + +} diff --git a/src/btp/oneP/GenericArrayTest.java b/src/btp/oneP/GenericArrayTest.java new file mode 100644 index 0000000..32a4978 --- /dev/null +++ b/src/btp/oneP/GenericArrayTest.java @@ -0,0 +1,26 @@ +package btp.oneP; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; + +public class GenericArrayTest { + + public static void main(String[] args) { + List[] las = new List[10]; + Object o = las; + Object[] oa = (Object[])o; + List li = new ArrayList(); + li.add(new Integer(3)); + oa[0] = li; + System.out.println(las[0].get(0)); + + List[] ls = (List[])new ArrayList[10]; + ls[0] = new ArrayList(); + ls[0].add("sss"); + System.out.println(ls[0].get(0)); + + Object an = Array.newInstance(ArrayList.class, 3); + } + +} diff --git a/src/btp/oneP/HeapOOM.java b/src/btp/oneP/HeapOOM.java new file mode 100644 index 0000000..1dcbafe --- /dev/null +++ b/src/btp/oneP/HeapOOM.java @@ -0,0 +1,17 @@ +package btp.oneP; + +import java.util.ArrayList; +import java.util.List; + +public class HeapOOM { + static class OOMObject{ + + } + public static void main(String[] args) { + List list = new ArrayList<>(); + while(true) { + list.add(new OOMObject()); + } + } + +} diff --git a/src/btp/oneP/InterfaceTest.java b/src/btp/oneP/InterfaceTest.java new file mode 100644 index 0000000..1a2bcdd --- /dev/null +++ b/src/btp/oneP/InterfaceTest.java @@ -0,0 +1,23 @@ +package btp.oneP; + +public class InterfaceTest { + + public static void main(String[] args) { + new InterfaceTest.Interface(){ + + @Override + public void get() { + // TODO Auto-generated method stub + + }}; + new InterfaceTest().new Class(); + } + + public interface Interface{ + void get(); + } + + class Class{ + + } +} diff --git a/src/btp/oneP/JoinTest.java b/src/btp/oneP/JoinTest.java new file mode 100644 index 0000000..e8d4e69 --- /dev/null +++ b/src/btp/oneP/JoinTest.java @@ -0,0 +1,117 @@ +package btp.oneP; + +public class JoinTest { + + public static void main(String[] args) throws InterruptedException { + Thread jt2 = new Thread(new JT2()); + Thread jt1 = new Thread(new JT1(jt2)); + + jt2.start(); + Thread.sleep(3000); + jt1.start(); + //jt1.interrupt();此时jt1没必要等jt2完成再继续 + Thread.sleep(20000); + } + +} + + +class JT1 implements Runnable { + private Thread t; + public JT1(Thread t) { + this.t= t; + } + + @Override + public void run() { + System.out.println("JT1 executing..."); + try { + //t.join(); + //t.join(1000L); + t.join(0, 0); + System.out.println("JT1 after JT2 join..."); + } catch (InterruptedException e) { + //Thread.currentThread().interrupt(); + System.out.println("JT1 interrupted..."); + } + + } + +} + +class JT2 implements Runnable{ + + @Override + public void run() { + /*while(true) {//这样的话ST1就永远不会继续往下执行了}*/ + System.out.println("JT2 executing..."); + try { + Thread.sleep(10000); + System.out.println("JT2 after sleep..."); + } catch (InterruptedException e) { + System.out.println("JT2 interrupted..."); + //Thread.currentThread().interrupt(); + } + } + +} + + +/** + * 等待调用join()方法的线程死亡,当前线程才继续往下运行。 + * 比如在线程t1代码中调用了t2.join(),则t1要等t2死亡之后才继续执行。 + * + * join()和join(0)效果一样。 + * + * @throws InterruptedException + * 如果线程代码中调用了join,线程被其它线程中断,会抛出此异常,且中断状态会被清除。 + * 其实就是如果t1中代码有t2.join(),那意思就是t1等待t2先完成再继续,但是中断在 + * 这里的意思是t1不想等t2了,就把t1中断了,t1就不必等t2完成再完成。 + */ +/*public final void join() throws InterruptedException { + join(0); +}*/ + + +/** + * 等待调用join(millis)方法的线程死亡millis毫秒,如果millis毫秒时间后调用线程还未死亡或者 + * millis毫秒时间内调用线程就死亡了,当前线程继续执行。 + * join(0)和join()效果一样,会一直等下去,直到调用线程死亡。 + * + * join(millis)使用循环检查this.isAlive(),判断调用线程是否死亡。而且join是使用wait + * 来实现的,所以其他线程调用调用线程的notifyAll()方法就会破坏join方法,所以推荐应用程序 + * 中不要使用线程作为对象锁。 + * + * @param millis + * 等待的最大时长 + * + * @throws IllegalArgumentException + * millis是负数 + * + * @throws InterruptedException + * 同join() + */ +/*public final synchronized void join(long millis) +throws InterruptedException { + long base = System.currentTimeMillis(); + long now = 0; + + if (millis < 0) { + throw new IllegalArgumentException("timeout value is negative"); + } + + if (millis == 0) { + while (isAlive()) { + wait(0); + } + } else { + while (isAlive()) { + long delay = millis - now; + if (delay <= 0) { + break; + } + wait(delay); + now = System.currentTimeMillis() - base; + } + } +}*/ \ No newline at end of file diff --git a/src/btp/oneP/PatternTest1.java b/src/btp/oneP/PatternTest1.java new file mode 100644 index 0000000..b72bbfd --- /dev/null +++ b/src/btp/oneP/PatternTest1.java @@ -0,0 +1,21 @@ +package btp.oneP; + +import java.io.UnsupportedEncodingException; +import java.util.regex.Pattern; + +public class PatternTest1 { + + public static void main(String[] args) throws UnsupportedEncodingException { + String pattern = "^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\\d{8}$"; + boolean isMatch = Pattern.matches(pattern, "15500005555"); + byte[] bytes = new byte[] {111, 114, 103, 97, 110, 73, 100, 61, 56, 56, 56, 56, 38, 105, 110, 102, 111, 84, 121, 112, 101, 49, 61, 49, 38, 105, 110, 102, 111, 84, 121, 112, 101, 50, 61, 48, 38, 99, 104, 97, 110, 110, 101, 108, 73, 100, 61, 48, 38, 99, 114, 101, 97, 116, 111, 114, 73, 100, 61, 110, 117, 108, 108, 38, 108, 120, 61, 49, 38, 99, 111, 110, 116, 101, 110, 116, 61, 37, 67, 48, 37, 69, 69, 37, 66, 54, 37, 70, 69, 37, 67, 66, 37, 65, 66, 37, 66, 56, 37, 70, 54, 50, 98, 38, 109, 111, 98, 105, 108, 101, 78, 117, 109, 61, 55, 55, 38, 117, 115, 101, 114, 73, 100, 61, 110, 117, 108, 108, 38, 115, 101, 110, 100, 76, 101, 118, 101, 108, 61, 52, 38, 117, 115, 101, 114, 78, 97, 109, 101, 61, 99, 102, 119, 120, 38, 112, 97, 115, 115, 87, 100, 61, 101, 49, 48, 97, 100, 99, 51, 57, 52, 57, 98, 97, 53, 57, 97, 98, 98, 101, 53, 54, 101, 48, 53, 55, 102, 50, 48, 102, 56, 56, 51, 101, 38, 106, 111, 105, 110, 84, 121, 112, 101, 61, 49, 38, 114, 101, 115, 101, 114, 118, 101, 100, 50, 61, 110, 117, 108, 108, 38, 114, 101, 115, 101, 114, 118, 101, 100, 51, 61, 110, 117, 108, 108, 38, 99, 108, 105, 101, 110, 116, 73, 80, 61, 49, 50, 55, 46, 48, 46, 48, 46, 49, 38, 114, 101, 115, 101, 114, 118, 101, 100, 49, 61, 48, 38, 109, 111, 98, 105, 108, 101, 61, 49, 51, 57, 54, 56, 48, 51, 53, 54, 57, 50, 44, 49, 53, 50, 53, 56, 56, 55, 53, 48, 48, 48, 44, 48, 53, 57, 50, 53, 55, 48, 57, 57, 55, 48, 44, 49, 51, 56, 53, 56, 48, 50, 56, 57, 56, 57, 44, 48, 53, 55, 56, 50, 50, 57, 50, 50, 53, 49, 44, 49, 51, 53, 56, 55, 48, 50, 56, 48, 54, 48, 44, 49, 51, 54, 48, 54, 53, 48, 56, 48, 53, 56, 44, 49, 51, 56, 53, 56, 54, 48, 48, 55, 56, 48, 44, 49, 51, 56, 54, 55, 51, 57, 53, 57, 48, 50, 44, 49, 51, 56, 49, 54, 54, 49, 50, 49, 57, 48, 44, 49, 51, 57, 53, 55, 48, 54, 56, 56, 55, 52, 44, 49, 53, 50, 54, 56, 49, 52, 49, 54, 51, 57, 44, 49, 51, 51, 57, 54, 55, 49, 56, 51, 50, 55, 44, 49, 51, 56, 53, 55, 54, 57, 57, 49, 53, 49, 44, 49, 51, 56, 49, 48, 48, 49, 54, 57, 52, 51, 44, 48, 53, 57, 50, 53, 51, 51, 48, 57, 55, 56, 44, 56, 54, 53, 57, 54, 48, 52, 48, 44, 48, 53, 57, 50, 53, 51, 49, 49, 50, 56, 54, 44, 49, 56, 56, 52, 52, 48, 50, 53, 51, 51, 51, 44, 49, 53, 56, 53, 55, 56, 49, 48, 55, 57, 55, 44, 49, 53, 48, 54, 55, 57, 49, 51, 51, 48, 55, 44, 49, 51, 56, 49, 54, 55, 50, 54, 57, 49, 57, 44, 49, 56, 57, 53, 56, 53, 50, 56, 55, 54, 56, 44, 49, 51, 55, 51, 54, 50, 54, 48, 50, 56, 56, 44, 49, 51, 53, 55, 53, 56, 50, 57, 50, 54, 53, 44, 49, 51, 57, 53, 55, 50, 57, 54, 55, 55, 50, 44, 49, 51, 56, 48, 53, 55, 52, 57, 57, 55, 53, 44, 49, 51, 57, 48, 53, 55, 54, 57, 50, 49, 49, 44, 49, 51, 57, 48, 54, 55, 53, 49, 56, 49, 51, 44, 49, 51, 54, 48, 54, 56, 50, 51, 49, 57, 54, 44, 49, 51, 56, 53, 55, 50, 48, 57, 48, 54, 52, 44, 49, 51, 56, 54, 55, 49, 51, 49, 54, 48, 56, 44, 49, 51, 51, 53, 55, 49, 51, 55, 52, 55, 55, 44, 49, 51, 56, 53, 55, 50, 55, 49, 55, 49, 57, 44, 49, 51, 57, 53, 56, 49, 53, 56, 55, 49, 50, 44, 49, 51, 54, 54, 54, 54, 55, 48, 57, 55, 49, 44, 49, 53, 57, 53, 55, 49, 49, 51, 48, 48, 48, 44, 49, 51, 56, 53, 55, 54, 54, 51, 48, 50, 55, 44, 49, 56, 57, 49, 56, 51, 55, 49, 57, 54, 50, 44, 49, 56, 57, 53, 55, 50, 55, 56, 53, 50, 57, 44, 49, 51, 56, 49, 57, 54, 53, 55, 51, 52, 55, 44, 49, 53, 48, 53, 56, 49, 57, 52, 51, 48, 49, 44, 49, 51, 57, 54, 56, 51, 54, 57, 57, 52, 57, 44, 49, 51, 57, 50, 56, 52, 55, 54, 54, 56, 57, 44, 49, 56, 54, 50, 49, 48, 49, 48, 49, 48, 56, 44, 48, 53, 55, 56, 51, 49, 51, 48, 57, 55, 49, 44, 49, 51, 55, 51, 53, 54, 53, 49, 48, 50, 50, 44, 49, 53, 57, 54, 56, 54, 49, 55, 48, 54, 54, 44, 49, 51, 54, 51, 49, 53, 53, 48, 52, 54, 48, 44, 49, 51, 53, 48, 54, 53, 57, 54, 53, 56, 56, 44, 49, 51, 55, 51, 54, 56, 57, 51, 51, 57, 48, 44, 49, 51, 57, 49, 48, 50, 56, 56, 48, 55, 50, 44, 49, 51, 54, 48, 53, 56, 49, 56, 50, 53, 57, 44, 49, 51, 57, 54, 55, 51, 51, 50, 56, 49, 51, 44, 49, 56, 54, 54, 55, 56, 48, 56, 56, 48, 48, 44, 49, 56, 48, 51, 56, 48, 51, 57, 50, 50, 54, 44, 49, 51, 55, 53, 52, 51, 50, 51, 52, 53, 54, 44, 49, 51, 53, 56, 56, 56, 48, 56, 51, 50, 54, 44, 49, 51, 56, 53, 55, 50, 55, 50, 50, 50, 51, 44, 49, 51, 57, 56, 57, 56, 55, 56, 53, 52, 49, 44, 49, 51, 51, 50, 50, 57, 57, 48, 51, 49, 49, 44, 49, 51, 53, 56, 55, 51, 48, 56, 50, 50, 56, 44, 49, 51, 55, 55, 55, 52, 48, 49, 54, 48, 49, 44, 49, 51, 53, 55, 56, 54, 54, 57, 50, 55, 55, 44, 49, 51, 56, 48, 54, 55, 56, 53, 51, 54, 55, 44, 49, 51, 55, 51, 53, 50, 49, 50, 49, 50, 49, 44, 49, 51, 51, 48, 54, 56, 49, 54, 49, 55, 55, 44, 49, 51, 55, 48, 53, 55, 53, 48, 52, 54, 56, 44, 49, 51, 54, 48, 54, 55, 53, 51, 54, 51, 48, 44, 49, 51, 53, 48, 53, 55, 57, 48, 57, 49, 48, 44, 49, 51, 57, 54, 56, 49, 49, 57, 50, 48, 49, 44, 49, 51, 53, 54, 54, 53, 51, 49, 51, 57, 54, 44, 49, 51, 51, 49, 49, 48, 54, 48, 52, 57, 57, 44, 49, 53, 56, 48, 48, 53, 50, 57, 57, 49, 52, 44, 49, 51, 53, 55, 53, 53, 55, 48, 52, 57, 48, 44, 49, 51, 56, 53, 56, 49, 51, 54, 57, 50, 48, 44, 49, 51, 57, 48, 54, 53, 55, 51, 48, 49, 51}; + + + byte[] bytes2 = new byte[] {108, 120, 61, 49, 38, 115, 101, 113, 117, 101, 110, 99, 101, 73, 68, 61, 38, 117, 115, 101, 114, 78, 97, 109, 101, 61, 99, 102, 119, 120, 38, 112, 97, 115, 115, 87, 100, 61, 101, 49, 48, 97, 100, 99, 51, 57, 52, 57, 98, 97, 53, 57, 97, 98, 98, 101, 53, 54, 101, 48, 53, 55, 102, 50, 48, 102, 56, 56, 51, 101, 38, 109, 111, 98, 105, 108, 101, 61, 49, 51, 57, 54, 56, 48, 51, 53, 54, 57, 50, 44, 49, 53, 50, 53, 56, 56, 55, 53, 48, 48, 48, 44, 48, 53, 57, 50, 53, 55, 48, 57, 57, 55, 48, 44, 49, 51, 56, 53, 56, 48, 50, 56, 57, 56, 57, 44, 48, 53, 55, 56, 50, 50, 57, 50, 50, 53, 49, 44, 49, 51, 53, 56, 55, 48, 50, 56, 48, 54, 48, 44, 49, 51, 54, 48, 54, 53, 48, 56, 48, 53, 56, 44, 49, 51, 56, 53, 56, 54, 48, 48, 55, 56, 48, 44, 49, 51, 56, 54, 55, 51, 57, 53, 57, 48, 50, 44, 49, 51, 56, 49, 54, 54, 49, 50, 49, 57, 48, 44, 49, 51, 57, 53, 55, 48, 54, 56, 56, 55, 52, 44, 49, 53, 50, 54, 56, 49, 52, 49, 54, 51, 57, 44, 49, 51, 51, 57, 54, 55, 49, 56, 51, 50, 55, 44, 49, 51, 56, 53, 55, 54, 57, 57, 49, 53, 49, 44, 49, 51, 56, 49, 48, 48, 49, 54, 57, 52, 51, 44, 48, 53, 57, 50, 53, 51, 51, 48, 57, 55, 56, 44, 56, 54, 53, 57, 54, 48, 52, 48, 44, 48, 53, 57, 50, 53, 51, 49, 49, 50, 56, 54, 44, 49, 56, 56, 52, 52, 48, 50, 53, 51, 51, 51, 44, 49, 53, 56, 53, 55, 56, 49, 48, 55, 57, 55, 44, 49, 53, 48, 54, 55, 57, 49, 51, 51, 48, 55, 44, 49, 51, 56, 49, 54, 55, 50, 54, 57, 49, 57, 44, 49, 56, 57, 53, 56, 53, 50, 56, 55, 54, 56, 44, 49, 51, 55, 51, 54, 50, 54, 48, 50, 56, 56, 44, 49, 51, 53, 55, 53, 56, 50, 57, 50, 54, 53, 44, 49, 51, 57, 53, 55, 50, 57, 54, 55, 55, 50, 44, 49, 51, 56, 48, 53, 55, 52, 57, 57, 55, 53, 44, 49, 51, 57, 48, 53, 55, 54, 57, 50, 49, 49, 44, 49, 51, 57, 48, 54, 55, 53, 49, 56, 49, 51, 44, 49, 51, 54, 48, 54, 56, 50, 51, 49, 57, 54, 44, 49, 51, 56, 53, 55, 50, 48, 57, 48, 54, 52, 44, 49, 51, 56, 54, 55, 49, 51, 49, 54, 48, 56, 44, 49, 51, 51, 53, 55, 49, 51, 55, 52, 55, 55, 44, 49, 51, 56, 53, 55, 50, 55, 49, 55, 49, 57, 44, 49, 51, 57, 53, 56, 49, 53, 56, 55, 49, 50, 44, 49, 51, 54, 54, 54, 54, 55, 48, 57, 55, 49, 44, 49, 53, 57, 53, 55, 49, 49, 51, 48, 48, 48, 44, 49, 51, 56, 53, 55, 54, 54, 51, 48, 50, 55, 44, 49, 56, 57, 49, 56, 51, 55, 49, 57, 54, 50, 44, 49, 56, 57, 53, 55, 50, 55, 56, 53, 50, 57, 44, 49, 51, 56, 49, 57, 54, 53, 55, 51, 52, 55, 44, 49, 53, 48, 53, 56, 49, 57, 52, 51, 48, 49, 44, 49, 51, 57, 54, 56, 51, 54, 57, 57, 52, 57, 44, 49, 51, 57, 50, 56, 52, 55, 54, 54, 56, 57, 44, 49, 56, 54, 50, 49, 48, 49, 48, 49, 48, 56, 44, 48, 53, 55, 56, 51, 49, 51, 48, 57, 55, 49, 44, 49, 51, 55, 51, 53, 54, 53, 49, 48, 50, 50, 44, 49, 53, 57, 54, 56, 54, 49, 55, 48, 54, 54, 44, 49, 51, 54, 51, 49, 53, 53, 48, 52, 54, 48, 44, 49, 51, 53, 48, 54, 53, 57, 54, 53, 56, 56, 44, 49, 51, 55, 51, 54, 56, 57, 51, 51, 57, 48, 44, 49, 51, 57, 49, 48, 50, 56, 56, 48, 55, 50, 44, 49, 51, 54, 48, 53, 56, 49, 56, 50, 53, 57, 44, 49, 51, 57, 54, 55, 51, 51, 50, 56, 49, 51, 44, 49, 56, 54, 54, 55, 56, 48, 56, 56, 48, 48, 44, 49, 56, 48, 51, 56, 48, 51, 57, 50, 50, 54, 44, 49, 51, 53, 56, 56, 56, 48, 56, 51, 50, 54, 44, 49, 51, 56, 53, 55, 50, 55, 50, 50, 50, 51, 44, 49, 51, 55, 53, 52, 51, 50, 51, 52, 53, 54, 44, 49, 51, 57, 56, 57, 56, 55, 56, 53, 52, 49, 44, 49, 51, 51, 50, 50, 57, 57, 48, 51, 49, 49, 44, 49, 51, 53, 56, 55, 51, 48, 56, 50, 50, 56, 44, 49, 51, 55, 55, 55, 52, 48, 49, 54, 48, 49, 44, 49, 51, 53, 55, 56, 54, 54, 57, 50, 55, 55, 44, 49, 51, 56, 48, 54, 55, 56, 53, 51, 54, 55, 44, 49, 51, 55, 51, 53, 50, 49, 50, 49, 50, 49, 44, 49, 51, 51, 48, 54, 56, 49, 54, 49, 55, 55, 44, 49, 51, 55, 48, 53, 55, 53, 48, 52, 54, 56, 44, 49, 51, 54, 48, 54, 55, 53, 51, 54, 51, 48, 44, 49, 51, 53, 48, 53, 55, 57, 48, 57, 49, 48, 44, 49, 51, 57, 54, 56, 49, 49, 57, 50, 48, 49, 44, 49, 51, 53, 54, 54, 53, 51, 49, 51, 57, 54, 44, 49, 51, 51, 49, 49, 48, 54, 48, 52, 57, 57, 44, 49, 53, 56, 48, 48, 53, 50, 57, 57, 49, 52, 44, 49, 51, 53, 55, 53, 53, 55, 48, 52, 57, 48, 44, 49, 51, 56, 53, 56, 49, 51, 54, 57, 50, 48, 44, 49, 51, 57, 48, 54, 53, 55, 51, 48, 49, 51, 38, 99, 111, 110, 116, 101, 110, 116, 61, -27, -123, -88, -28, -72, -106, -25, -107, -116, -23, -125, -67, -25, -120, -79, -24, -81, -76, -28, -72, -83, -26, -106, -121, -17, -68, -116, -28, -72, -115, -25, -120, -79, -24, -81, -76, 101, 110, 103, 108, 105, 115, 104, -29, -128, -126, 38, 99, 104, 97, 110, 110, 101, 108, 73, 100, 61, 48, 38, 109, 111, 98, 105, 108, 101, 67, 104, 97, 110, 110, 101, 108, 73, 100, 61, 38, 117, 110, 105, 99, 111, 109, 67, 104, 97, 110, 110, 101, 108, 73, 100, 61, 38, 116, 101, 108, 99, 111, 109, 67, 104, 97, 110, 110, 101, 108, 73, 100, 61, 38, 105, 110, 116, 101, 114, 70, 97, 99, 101, 83, 101, 110, 100, 84, 105, 109, 101, 61, 38, 99, 114, 101, 97, 116, 111, 114, 73, 100, 61, 38, 111, 114, 103, 97, 110, 73, 100, 61, 56, 56, 56, 56, 38, 117, 115, 101, 114, 73, 100, 61, 38, 105, 110, 102, 111, 84, 121, 112, 101, 61, 38, 105, 110, 102, 111, 84, 121, 112, 101, 49, 61, 49, 38, 105, 110, 102, 111, 84, 121, 112, 101, 50, 61, 48, 38, 114, 101, 115, 101, 114, 118, 101, 100, 49, 61, 48, 38, 114, 101, 115, 101, 114, 118, 101, 100, 50, 61, 38, 114, 101, 115, 101, 114, 118, 101, 100, 51, 61, 38, 105, 115, 87, 65, 80, 61, 38, 119, 97, 112, 85, 82, 76, 61, 38, 115, 101, 110, 100, 76, 101, 118, 101, 108, 61, 52, 38, 116, 114, 121, 84, 105, 109, 101, 115, 61, 38, 114, 101, 99, 101, 105, 118, 101, 84, 105, 109, 101, 61, 38, 99, 108, 105, 101, 110, 116, 73, 80, 61, 49, 50, 55, 46, 48, 46, 48, 46, 49, 38, 109, 111, 98, 105, 108, 101, 78, 117, 109, 61, 55, 55, 38, 99, 111, 110, 116, 114, 111, 108, 70, 108, 97, 103, 61}; + String string = new String(bytes); + String string2 = new String(bytes2,"utf-8"); + System.out.println(string); + System.out.println(string2); + } + +} diff --git a/src/btp/oneP/SleepTest.java b/src/btp/oneP/SleepTest.java new file mode 100644 index 0000000..95e892a --- /dev/null +++ b/src/btp/oneP/SleepTest.java @@ -0,0 +1,70 @@ +package btp.oneP; + +public class SleepTest { + + public static void main(String[] args) throws InterruptedException { + Object o = new Object(); + Thread t1 = new Thread(new ST1(o)); + Thread t2 = new Thread(new ST2(o)); + t1.start(); + Thread.sleep(4000); + t2.start(); + Thread.sleep(20000); + } + +} + +class ST1 implements Runnable{ + private Object o; + public ST1(Object o) { + this.o = o; + } + @Override + public void run() { + System.out.println("ST1 get CPU..."); + synchronized(o) { + System.out.println("ST1获得锁..."); + try { + Thread.sleep(10000L); + System.out.println("ST1 after sleep and get CPU"); + } catch (InterruptedException e) { + System.out.println("ST1被中断..."); + } + System.out.println("ST1准备释放锁..."); + } + System.out.println("ST1 realease CPU..."); + } + +} + +class ST2 implements Runnable{ + private Object o; + public ST2(Object o) { + this.o = o; + } + @Override + public void run() { + System.out.println("ST2 get CPU..."); + synchronized(o) { + System.out.println("ST2获得锁..."); + } + System.out.println("ST2 realease CPU..."); + } + +} + + +/** + * 使当前的线程停止millis毫秒的时间。 + * 线程调用sleep(millis)并不会失去锁的持有权。 + * + * @param millis + * 线程停止执行的毫秒数 + * + * @throws IllegalArgumentException + * millis为负数 + * + * @throws InterruptedException + * 如果线程被其他线程中断,则线程的中断状态会被清除并抛出此异常。 + */ +/*public static native void sleep(long millis) throws InterruptedException;*/ diff --git a/src/btp/oneP/SystemevnTest.java b/src/btp/oneP/SystemevnTest.java new file mode 100644 index 0000000..c343d56 --- /dev/null +++ b/src/btp/oneP/SystemevnTest.java @@ -0,0 +1,27 @@ +package btp.oneP; + +import java.util.Calendar; +import java.util.Date; +import java.util.Map; + +public class SystemevnTest { + + public static void main(String[] args) { + Map sysevn = System.getProperties(); + + for(Object key : sysevn.keySet()) { + /*if("RUNTIME_CONFIG_ROOT".equals(key)) { + System.out.println(sysevn.get(key)); + }*/ + System.out.println(key+" -> "+sysevn.get(key)); + } + + Calendar cd = Calendar.getInstance(); + Date today = new Date(); + System.out.println(today.getTime()); + cd.setTime(today); + cd.add(Calendar.MONTH, 1); + System.out.println(cd.getTime().getTime()); + } + +} diff --git a/src/btp/oneP/TestAB.java b/src/btp/oneP/TestAB.java new file mode 100644 index 0000000..483d053 --- /dev/null +++ b/src/btp/oneP/TestAB.java @@ -0,0 +1,39 @@ +package btp.oneP; + +public class TestAB { + + public static void main(String[] args) { + System.out.println(new B().getValue()); + } + static class A{ + protected int value; + + public A(int v) { + setValue(v); + } + public void setValue(int value) { + this.value = value; + } + public int getValue() { + try { + value++; + return value; + }catch(Exception e) { + System.out.println(e); + }finally { + this.setValue(value); + System.out.println(value); + } + return value; + } + } + static class B extends A{ + public B() { + super(5); + setValue(getValue() - 3); + } + public void setValue(int value) { + super.setValue(2*value); + } + } +} diff --git a/src/btp/oneP/Thread3.java b/src/btp/oneP/Thread3.java new file mode 100644 index 0000000..c94d8d2 --- /dev/null +++ b/src/btp/oneP/Thread3.java @@ -0,0 +1,125 @@ +package btp.oneP; + +public class Thread3 extends Thread { + public void run() { + while(true) { + if(Thread.currentThread().isInterrupted()) { + System.out.println("Someone interrupted me"); + break; + }else { + System.out.println("Thread is going..."); + + } + } + } + + public static void main(String...args) throws InterruptedException { + Thread3 t = new Thread3(); + t.start(); + Thread.currentThread().sleep(3000); + t.interrupt(); + } + + /** + * 如果没有其它线程调用对象锁的Object#notify()或者Object#notifyAll()方法,那调用了同一个 + * 对象锁的Object#wait()方法的线程会一直等待。 + * 调用Object#wait()和调用Object#wait(0)效果是一样的。 + * 调用wait()方法必须先获得对象锁。wait()方法的作用是释放当前拥有的对象锁并一直等待, + * 直到有其它线程使用同一个对象锁调用notify()方法或notifyAll()方法才可能继续执行wait() + * 之后的代码。 + * 有一种说法认为,执行wait()方法之后,可能会有中断或者虚假的唤醒,所以wait()方法一 + * 般要放在一个循环中(一般wait()方法的调用是伴随一个条件的,条件发生时才调用wait(), + * 中断或者虚假唤醒可能发生在这个条件满足的情况下,所以如果中断或者虚假唤醒了,要重 + * 新检查这个条件是否满足,满足的话就重新调用wait()): + * synchronized (obj) { + * while (<condition does not hold>) + * obj.wait(); + * ... // Perform action appropriate to condition + * } + * 切记,wait()方法必须被拥有对象锁的线程调用。可以查看notify()方法来了解线程获得对象 + * 锁的多种方式。 + * + * @throws IllegalMonitorStateException 调用wait()方法的线程没有拥有 + * 对象锁。 + * @throws InterruptedException 线程在调用wait()方法之前或wait()方法等待中 + * 被其他线程中断了,就会抛出这个异常并重置中断状态位。 + * @see java.lang.Object#notify() + * @see java.lang.Object#notifyAll() + */ + /* public final void wait() throws InterruptedException { + wait(0); + }*/ + + /** + * 如果没有其它线程调用对象锁的Object#notify()或者Object#notifyAll()方法,那调用了同一个 + * 对象锁的Object#wait(timeout)方法的线程会一直等待;timeout长的时间过后同样会停止等待。 + * 调用wait(timeout)方法的线程必须现获得对象锁。 + * 这个方法会把调用它的线程放入对象锁的等待集合中,并且唤醒在这个对象锁上等待的对象。线程会停止执行余下的 + * 代码并陷入休眠。直到下面四个条件发生其一: + * ①有线程调用了这个对象锁的notify()方法,并且刚好是休眠线程被选中唤醒。 + * ②有线程调用了这个对象锁的notifyAll()方法。 + * ③有线程调用了休眠线程的interrupt()方法。 + * ④过去了参数timeout长的时间。如果timeout是0,那线程会一直等待,wait(0)和wait()效果一样。 + * 上面四个条件有一个发生了,那休眠线程就会从对象锁的等待集合中移去,它会和其它等待对象锁上的线程一起 + * 竞争这个对象锁的使用权。一旦它获得了锁,它就会被恢复为wait(timeout)前的状态,当过去timeout长的 + * 时间也一样。线程会继续执行wait(timeout)之后的代码。 + * + * 除了被notify()/notifyAll()方法、中断和超时唤醒以外,线程也会被所谓的虚假唤醒。虽然这种情况在现实中 + * 很少发生,但是程序必须检查线程被唤醒的条件是否满足,如果不满足就应该继续让程序休眠。所以wait(timeout) + * 方法最好在一个循环中被调用,这样的话,线程被唤醒也可以检查条件是否满足: + * synchronized (obj) { + * while (<condition does not hold>) + * obj.wait(timeout); + * ... // Perform action appropriate to condition + * } + * 如果线程被java.lang.Thread#interrupt()中断之后调用wait(timeout)或者调用wait(timeout)休眠中被 + * java.lang.Thread#interrupt()中断,就会抛出InterruptedException。这个异常在对象 + * 锁被重置的时候抛出。 + * + * wait方法只能用于解除当前对象锁;其它可以同步当前线程的对象在线程休眠时依然可以处于锁定状态。 + * 切记,wait(timeout)方法必须被拥有对象锁的线程调用。可以查看notify()方法来了解线程获得对象 + * 锁的多种方式。 + * + * @param timeout 等待的最大时间数。 + * @throws IllegalArgumentException 当timeout为负数。 + * @throws IllegalMonitorStateException 当调用wait(timeout)方法的线程没有获得对象锁。 + * @throws InterruptedException 如果线程被java.lang.Thread#interrupt()中断之后 + * 调用wait(timeout)或者调用wait(timeout)休眠中被 + * java.lang.Thread#interrupt()中断抛出此异常, + * 并且重置线程的中断状态。 + * @see java.lang.Object#notify() + * @see java.lang.Object#notifyAll() + */ + /* public final native void wait(long timeout) throws InterruptedException;*/ + + + /** + * 唤醒一个在对象锁上休眠的线程,如果有多个休眠的线程,任意唤醒其中一个。在对象锁上休眠的线程指的是 + * 调用了对象锁的wait方法(三个wait方法中的任意一个)的线程。 + * 被唤醒的线程只有在当前线程放弃对象锁之后才可能继续执行,被唤醒的线程会和其它等待锁的线程(等待执行synchronized) + * 一起竞争对象锁的拥有权,竞争是公平的,不会有特权也不会有劣势。 + * 此方法必须在拥有对象锁的前提下执行,线程获得对象锁的方式有三种: + * ①执行对象锁的同步方法 + * ②执行对象锁的同步代码块。 + * ③如果对象锁是Class类型,执行此类的静态方法。 + * 一个对象锁某一时刻只能被一个线程持有。 + * + * @throws IllegalMonitorStateException 当调用notify()方法的线程不是对象锁的拥有者。 + * @see java.lang.Object#notifyAll() + * @see java.lang.Object#wait() + */ + /*public final native void notify();*/ + + /** + * 唤醒所有在对象锁上休眠的线程。在对象锁上休眠的线程指的是 + * 调用了对象锁的wait方法(三个wait方法中的任意一个)的线程。 + * 被唤醒的线程只有在当前线程放弃对象锁之后才可能继续执行,被唤醒的线程会和其它等待锁的线程(等待执行synchronized) + * 一起竞争对象锁的拥有权,竞争是公平的,不会有特权也不会有劣势。 + * 此方法必须在拥有对象锁的前提下执行,线程获得对象锁的方式间Object#notify()方法。 + * + * @throws IllegalMonitorStateException 当调用notify()方法的线程不是对象锁的拥有者。 + * @see java.lang.Object#notify() + * @see java.lang.Object#wait() + */ + /*public final native void notifyAll();*/ +} diff --git a/src/btp/oneP/Thread4.java b/src/btp/oneP/Thread4.java new file mode 100644 index 0000000..6d1a52a --- /dev/null +++ b/src/btp/oneP/Thread4.java @@ -0,0 +1,34 @@ +package btp.oneP; + +public class Thread4 extends Thread { + protected volatile boolean stop; + public static void main(String...args) throws InterruptedException { + Thread4 t = new Thread4(); + t.start(); + Thread.sleep(20000); + t.stop = true; + + + t.interrupt(); + + Thread.sleep(20000); + } + @Override + public void run() { + while(true) { + System.out.println("stop="+stop); + if(!stop) { + System.out.println("working..."); + try { + Thread.sleep(1000000); + } catch (InterruptedException e) { + System.out.println("interrupted..."); + } + }else { + System.out.println("break..."); + break; + } + + } + } +} diff --git a/src/btp/oneP/Thread5.java b/src/btp/oneP/Thread5.java new file mode 100644 index 0000000..bd63982 --- /dev/null +++ b/src/btp/oneP/Thread5.java @@ -0,0 +1,18 @@ +package btp.oneP; + +public class Thread5 extends Thread { + + public static void main(String[] args) throws InterruptedException { + Thread5 t5 = new Thread5(); + t5.start(); + Thread.sleep(10000); + t5.interrupt(); + Thread.sleep(10000); + } + + @Override + public void run() { + while(true) + System.out.println("run..."); + } +} diff --git a/src/btp/oneP/Thread6.java b/src/btp/oneP/Thread6.java new file mode 100644 index 0000000..d2f7498 --- /dev/null +++ b/src/btp/oneP/Thread6.java @@ -0,0 +1,106 @@ +package btp.oneP; + +public class Thread6 { + public static void main(String...args) throws InterruptedException { + Object o = new Object(); + /*创建6个线程,1个ET1和5个ET2*/ + ET1 et1 = new ET1(o); + Thread t1 = new Thread(et1); + Thread t2_1 = new Thread(new ET2(o,et1)); + Thread t2_2 = new Thread(new ET2(o,et1)); + Thread t2_3 = new Thread(new ET2(o,et1)); + Thread t2_4 = new Thread(new ET2(o,et1)); + Thread t2_5 = new Thread(new ET2(o,et1)); + //先让t1执行 + t1.start(); + Thread.sleep(5000); + t2_1.start();t2_2.start();t2_3.start();t2_4.start();t2_5.start(); + t1.interrupt(); + //五个线程中会有一个线程会先获得锁,这个线程执行完会唤醒wait的t1并且将其休眠条件置为true,让t1和剩下的四个线程一起竞争锁。 + Thread.sleep(20000); + } +} + +class ET1 implements Runnable{ + //线程的休眠条件,只要condition为false,线程就要休眠 + private boolean condition; + + /** + * 锁对象 + */ + Object o; + public ET1(Object o) { + this.o = o; + } + @Override + public void run() { + synchronized(o) { + System.out.println("ET1获得锁..."); + try { + //防止虚假唤醒(虚假唤醒的时候condition不一定满足),所以重复检查条件 + while(!condition) { + o.wait(); + } + + System.out.println("ET1 after wait..."); + } catch (InterruptedException e) { + System.out.println("ET1被中断..."); + //被中断也要检查条件,不满足也要继续休眠 + while(!condition) { + try { + System.out.println("ET1被中断但是条件不满足,继续休眠..."); + o.wait(); + } catch (InterruptedException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + } + } + System.out.println("ET1执行完毕释放锁..."); + } + public boolean isCondition() { + return condition; + } + public void setCondition(boolean condition) { + this.condition = condition; + } + +} + +class ET2 implements Runnable{ + public static int count = 0; + private int no; + private ET1 et1; + + /** + * 锁对象 + */ + Object o; + public ET2(Object o,ET1 et1) { + this.o = o; + this.et1 = et1; + this.no = count++; + } + @Override + public void run() { + System.out.println(this+"等待获得对象锁..."); + synchronized(o) { + System.out.println(this+"获得对象锁..."); + o.notify(); + //设置et1的条件为true,这样et1就不会进入休眠 + et1.setCondition(true); + System.out.println(this+"释放锁"); + } + } + public int getNo() { + return no; + } + public void setNo(int no) { + this.no = no; + } + @Override + public String toString() { + return "ET2 [no=" + no + "]"; + } +} \ No newline at end of file diff --git a/src/btp/oneP/ThreadLocalTest.java b/src/btp/oneP/ThreadLocalTest.java new file mode 100644 index 0000000..364e293 --- /dev/null +++ b/src/btp/oneP/ThreadLocalTest.java @@ -0,0 +1,41 @@ +package btp.oneP; + +import java.util.concurrent.Executors; + +public class ThreadLocalTest { + + public static void main(String[] args) throws InterruptedException { + Integer o = new Integer(100); + ThreadLocal1 tl1 = new ThreadLocal1(o); + ThreadLocal1 tl2 = new ThreadLocal1(o); + Thread t1 = new Thread(tl1); + Thread t2 = new Thread(tl2); + t1.start(); + t2.start(); + Thread.sleep(10000); + + Executors.newCachedThreadPool(); + Executors.newFixedThreadPool(0); + } + +} + + +class ThreadLocal1 implements Runnable{ + private final ThreadLocal tl = new ThreadLocal() { + @Override + protected Integer initialValue() { + Integer conn = null; + return conn; + } + }; + public ThreadLocal1(Integer o) { + System.out.println("---set"+o); + tl.set(o); + } + @Override + public void run() { + System.out.println(tl); + System.out.println(Thread.currentThread()+":"+tl.get()); + } +} \ No newline at end of file diff --git a/src/btp/oneP/WaitTest.java b/src/btp/oneP/WaitTest.java new file mode 100644 index 0000000..7e6937f --- /dev/null +++ b/src/btp/oneP/WaitTest.java @@ -0,0 +1,100 @@ +package btp.oneP; + +public class WaitTest { + + public static void main(String[] args) throws InterruptedException { + Object o = new Object(); + T1 t1 = new T1(o); + T2 t2 = new T2(o); + T3 t3 = new T3(o); + Thread t = new Thread(t1); + Thread tt = new Thread(t2); + Thread ttt = new Thread(t3); + System.out.println(t.getState()); + t.start(); + System.out.println(t.getState()); + //tt.start(); + //ttt.start(); + //t.interrupt(); + Thread.sleep(10000); + System.out.println(t.getState()); + } + +} + +class T1 implements Runnable{ + Object o; + public T1(Object o) { + this.o = o; + } + @Override + public void run() { + synchronized(o) { + System.out.println("T1获得锁"); + try { + //Thread.currentThread().interrupt(); + o.wait(); + + //o.notify(); + System.out.println("t1 after wait"); + } catch (InterruptedException e) { + System.out.println("*****"); + Thread.currentThread().interrupt(); + } + System.out.println("T1----"); + o.notify(); + } + + } + +} + +class T2 implements Runnable{ + Object o; + public T2(Object o) { + this.o = o; + } + @Override + public void run() { + synchronized(o) { + System.out.println("T2获得锁"); + try { + o.notifyAll(); + o.wait(); + + System.out.println("t2 after wait"); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println("T2----"); + } + + + } + +} + +class T3 implements Runnable{ + Object o; + public T3(Object o) { + this.o = o; + } + @Override + public void run() { + synchronized(o) { + System.out.println("T3获得锁"); + try { + o.notifyAll(); + System.out.println("t3 after wait"); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println("T3----"); + } + + + } + +} diff --git a/src/btp/oneP/YieldTest.java b/src/btp/oneP/YieldTest.java new file mode 100644 index 0000000..6cbe5de --- /dev/null +++ b/src/btp/oneP/YieldTest.java @@ -0,0 +1,46 @@ +package btp.oneP; + +public class YieldTest { + + public static void main(String[] args) throws InterruptedException { + Thread yt1 = new Thread(new YT1()); + Thread yt2 = new Thread(new YT2()); + yt1.start(); + + yt2.start(); + Thread.sleep(20000); + } + +} + +class YT1 implements Runnable{ + + @Override + public void run() { + System.out.println("YT1 get CPU..."); + Thread.yield(); + System.out.println("YT1 after yield..."); + } + +} + +class YT2 implements Runnable{ + + @Override + public void run() { + System.out.println("YT2 get CPU..."); + Thread.yield(); + System.out.println("YT2 after yield..."); + } + +} +/** + * 暗示调度程序调用此方法的线程愿意让出当前的处理器使用权,处理程序可以忽略此暗示(不理会)。 + * + * yield()是一种建议性的尝试,是为了改善线程间的轮转执行,以免过度使用CPU。使用此方法 + * 应该结合详细的分析和测试以保证能有你想要的效果。 + * + * 很少会用到此方法,它可能在调试和测试方面比较有用,它可以去重现一些竞态条件引起的bug, + * 它也在设计 java.util.concurrent.locks之类的包时被用到。 + */ +/*public static native void yield();*/ \ No newline at end of file diff --git a/src/btp/oneP/YongkangMeatWheatCakeTest.java b/src/btp/oneP/YongkangMeatWheatCakeTest.java new file mode 100644 index 0000000..7efda6f --- /dev/null +++ b/src/btp/oneP/YongkangMeatWheatCakeTest.java @@ -0,0 +1,142 @@ +package btp.oneP; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +/** + * 鐜板湪鏈夊緢澶氬崠姘稿悍鑲夐害楗肩殑搴楀瓙锛屼竴涓仛楗肩殑甯堝倕锛屽彴瀛愪笂鏈夌洏瀛愶紝鍋囧畾鐩樺瓙鍙兘鏀句竴瀹氭暟閲忕殑 + * 楗硷紝杩樻湁寰堝瀹㈡埛鏉ヤ拱銆 + * @author baitp + * + */ +public class YongkangMeatWheatCakeTest { + + public static void main(String[] args) throws InterruptedException { + //楗肩洏瀛 + BlockingQueue bq = new ArrayBlockingQueue(10); + //涓涓笀鍌 + Thread saysay = new Thread(new Craftsman(bq)); + //澶氫釜瀹㈡埛 + Thread c1 = new Thread(new Customer(bq)); + Thread c2 = new Thread(new Customer(bq)); + Thread c3 = new Thread(new Customer(bq)); + Thread c4 = new Thread(new Customer(bq)); + Thread c5 = new Thread(new Customer(bq)); + //鍚姩 + saysay.start(); + c1.start();c2.start();c3.start();c4.start();c5.start(); + Thread.sleep(20000); + } +} + +/** + * 鍋氶ゼ甯堝倕 + * + */ +class Craftsman implements Runnable{ + /** + * 鏀鹃ゼ鐨勭洏瀛愶紝甯堝倕寰閲岄潰鏀鹃ゼ + */ + private BlockingQueue bq; + + public Craftsman(BlockingQueue bq) { + this.bq = bq; + } + + @Override + public void run() { + //甯堝倕瑕佷竴鐩村仛楗硷紝鎵浠ユ斁鍦ㄥ惊鐜腑 + while(true) { + //鍥犱负楗肩洏鏄敮涓鐨勶紝浣跨敤瀹冧綔涓哄璞¢攣 + synchronized(bq) { + //楗肩洏婊$殑鏃跺欙紝甯堝倕浼戞伅銆 + while(bq.remainingCapacity() == 0) { + try { + System.out.println("鐩樺瓙婊′簡锛屽笀鍌呬紤鎭..."); + bq.wait(); + } catch (InterruptedException e) { + System.out.println("甯堝倕琚墦鏂簡..."); + } + } + //甯堝倕寰鐩樺瓙閲屾斁楗 + YongkangMeatWheatCake makedCake = new YongkangMeatWheatCake(); + bq.add(makedCake); + System.out.println("甯堝倕鍋氫簡涓"+makedCake+"鏀惧叆鐩樺瓙涓"); + //鍞ら啋鍥犱负楗肩洏绌鸿岀瓑鍒扮殑椤惧锛屽拰鍒氭潵鐨勯【瀹竴璧风珵浜変拱楗硷紙鍋囪涓嶆帓闃熷惂(`銇嘎*)銉庯級 + bq.notifyAll(); + } + } + } + +} + +/** + * 娑堣垂鑰 + * + */ +class Customer implements Runnable{ + public static int count = 0; + private int no; + /** + * 鏀鹃ゼ鐨勭洏瀛愶紝娑堣垂鑰呬粠閲岄潰鎷块ゼ + */ + private BlockingQueue bq; + + /** + * 涔板埌鐨勯ゼ + */ + private YongkangMeatWheatCake myCake; + + public Customer(BlockingQueue bq) { + this.bq = bq; + no = ++count; + } + + @Override + public void run() { + System.out.println(this+"鏉ヤ拱楗间簡..."); + //鍥犱负楗肩洏鏄敮涓鐨勶紝浣跨敤瀹冧綔涓哄璞¢攣 + synchronized(bq) { + System.out.println(this+"鍑嗗涔伴ゼ..."); + //鐩樺瓙绌虹殑鏃跺欙紝瀹㈡埛绛夊緟 + while(bq.isEmpty()) { + try { + System.out.println("楗肩洏绌轰簡锛"+this+"绛夊緟..."); + //鐩樺瓙绌虹殑鏃跺欙紝鍙啋鍏朵粬绛夊緟鐨勭嚎绋嬶紝杩欏叾涓寘鎷笀鍌咃紝甯堝倕灏卞彲浠ュ仛楗 + bq.notifyAll(); + bq.wait(); + } catch (InterruptedException e) { + System.out.println("椤惧琚腑鏂簡..."); + } + } + myCake = bq.poll(); + System.out.println("鍝囷紒"+this+"涔板埌浜"+myCake); + //閫氱煡甯堝倕 + bq.notifyAll(); + } + } + + @Override + public String toString() { + return "Customer"+no; + } + +} + +/** + * 姘稿悍鑲夐害楗肩被 + */ +class YongkangMeatWheatCake{ + public static int count = 0; + private int cakeNo; + public YongkangMeatWheatCake() { + cakeNo = ++count; + } + + @Override + public String toString() { + return "楗 " + cakeNo; + } + +} +