Merge branch 'master' of https://github.com/xiongraorao/Interview-Notebook
This commit is contained in:
31
code/src/main/java/com/raorao/Test.java
Normal file
31
code/src/main/java/com/raorao/Test.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.raorao;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-02-16:46
|
||||
*/
|
||||
public class Test {
|
||||
|
||||
private int a;
|
||||
|
||||
public static void main(String[] args) {
|
||||
//System.out.println(foo());
|
||||
int r = new Test().foo();
|
||||
System.out.println(r);
|
||||
}
|
||||
|
||||
public int foo() {
|
||||
try {
|
||||
System.out.println("sadfasdfasdf");
|
||||
return a;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 3;
|
||||
} finally {
|
||||
System.out.println("finally: " + ++a);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
}
|
28
code/src/main/java/com/raorao/java/base/InnerClassTest.java
Normal file
28
code/src/main/java/com/raorao/java/base/InnerClassTest.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.raorao.java.base;
|
||||
|
||||
/**
|
||||
* 内部类的引用.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-06-14:43
|
||||
*/
|
||||
public class InnerClassTest {
|
||||
void f(){
|
||||
System.out.println("InnerClassTest.f()");
|
||||
}
|
||||
public class Inner{
|
||||
public InnerClassTest outer(){
|
||||
return InnerClassTest.this;
|
||||
}
|
||||
}
|
||||
|
||||
public Inner inner(){
|
||||
return new Inner();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
InnerClassTest test = new InnerClassTest();
|
||||
InnerClassTest.Inner inner = test.new Inner();
|
||||
//inner = new InnerClassTest.Inner(); // 如果Inner 是static就可以,否则只能采用上面的语句
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package com.raorao.java.thread;
|
||||
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 可重入锁测试.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-06-10:27
|
||||
*/
|
||||
public class ReentrantLockTest {
|
||||
|
||||
private Lock lock = new ReentrantLock();
|
||||
private Condition conditionA = lock.newCondition();
|
||||
private Condition conditionB = lock.newCondition();
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ReentrantLockTest test = new ReentrantLockTest();
|
||||
new Thread(() -> test.testLock()).start();
|
||||
new Thread(() -> test.testLock()).start();
|
||||
|
||||
Thread t = new Thread(() -> test.awaitA());
|
||||
t.start();
|
||||
Thread.sleep(2000);
|
||||
test.signalA();
|
||||
|
||||
}
|
||||
|
||||
public void awaitA() {
|
||||
lock.lock();
|
||||
try {
|
||||
System.out.println("before awaitA at " + System.currentTimeMillis());
|
||||
conditionA.await(); // 在此之前必须获得锁,不然报错illegalMonitorStateException 错误
|
||||
System.out.println("after awaitA at " + System.currentTimeMillis());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
System.out.println(" 释放锁 awaitA ");
|
||||
}
|
||||
}
|
||||
|
||||
public void signalA() {
|
||||
lock.lock();
|
||||
try {
|
||||
System.out.println("signalA at " + System.currentTimeMillis());
|
||||
conditionA.signal(); // 在此之前必须获得锁,不然报错illegalMonitorStateException 错误
|
||||
System.out.println("signalA over at " + System.currentTimeMillis());
|
||||
} finally {
|
||||
lock.unlock();
|
||||
System.out.println(" 释放锁 signalA ");
|
||||
}
|
||||
}
|
||||
|
||||
public void testLock() {
|
||||
lock.lock();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
System.out.println();
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.raorao.java.thread;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
/**
|
||||
* 读写锁测试.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-06-11:48
|
||||
*/
|
||||
public class ReentrantReadWriteLockTest {
|
||||
|
||||
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
|
||||
public void read(){
|
||||
lock.readLock().lock(); // 读锁
|
||||
System.out.println("获得读锁 " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void write(){
|
||||
lock.writeLock().lock(); // 写锁
|
||||
System.out.println("获得写锁 " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 1. 读读共享, A 和 B 同时获得锁
|
||||
ReentrantReadWriteLockTest test = new ReentrantReadWriteLockTest();
|
||||
Thread t1 = new Thread(()-> {
|
||||
Thread.currentThread().setName("A");
|
||||
test.read();
|
||||
});
|
||||
Thread t2 = new Thread(()-> {
|
||||
Thread.currentThread().setName("B");
|
||||
test.read();
|
||||
});
|
||||
t1.start();
|
||||
t2.start();
|
||||
|
||||
// 2. 写写互斥, D线程比C线程落后两秒执行
|
||||
t1 = new Thread(()->{
|
||||
Thread.currentThread().setName("C");
|
||||
test.write();
|
||||
});
|
||||
t2 = new Thread(()->{
|
||||
Thread.currentThread().setName("D");
|
||||
test.write();
|
||||
});
|
||||
t1.start();
|
||||
t2.start();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2018. Xiong Raorao. All rights reserved.
|
||||
* Project Name: book-notes
|
||||
* File Name: Test.java
|
||||
* Date: 18-7-25 下午5:32
|
||||
* Author: Xiong Raorao
|
||||
*/
|
||||
|
||||
package com.raorao.java.thread;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-07-25-17:32
|
||||
*/
|
||||
public class ThreadLocalTest {
|
||||
private static ThreadLocal<Integer> local = new ThreadLocal<>();
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread t1 = new Thread(() -> {
|
||||
System.out.println(" I am t1");
|
||||
local.set(1);
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
System.out.println("t1 value: " + local.get());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
Thread.sleep(2000);
|
||||
Thread t2 = new Thread(() -> {
|
||||
System.out.println("I am t2");
|
||||
System.out.println("before set , I get " + local.get());
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
local.set(2);
|
||||
System.out.println("set after 2 s, I get " + local.get());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
t2.start();
|
||||
t1.start();// 无论怎么更换两个线程的启动顺序,得到的值是不一样的
|
||||
}
|
||||
}
|
36
code/src/main/java/com/raorao/java/thread/TimerTest.java
Normal file
36
code/src/main/java/com/raorao/java/thread/TimerTest.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.raorao.java.thread;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* 定时器.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-06-13:07
|
||||
*/
|
||||
public class TimerTest {
|
||||
private static Timer timer = new Timer();
|
||||
|
||||
static class MyTask extends TimerTask{
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("运行时间: " + new Date());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
MyTask task1 = new MyTask();
|
||||
try {
|
||||
Date taskDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2018-08-06 13:15:00");
|
||||
System.out.println("执行时间: " + taskDate.toLocaleString() + ",当前时间" + new Date().toLocaleString());
|
||||
timer.schedule(task1, taskDate);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user