???????Thread.start()??Thread.run()??????
???????????Thread???start()???????????????????????????????????????????С?????????Thread????÷???run()??????????в????????????run()???????壬???????????е?????????????Run???????н?????????????????CPU??????????????
??????????????Run??????????????????????????????????????“?????”?????????????п?????????????????·????????????????????д?д????????
???????????????
????public class MyThread implements Runnable {
????public void run() {
????System.err.println(Thread.currentThread().getName());
????}
????public static void main(String[] args) {
????MyThread thread = new MyThread();
????Thread t1 = new Thread(thread?? "Thread-1");
????Thread t2 = new Thread(thread?? "Thread-2");
????t1.run();
????t2.run();
????}
????}
???????????
????>>??????????main
????>>??????????main
?????????start??????
????package thread;
????public class MyThread implements Runnable {
????public void run() {
????System.err.println(">>??????????"+Thread.currentThread().getName());
????}
????public static void main(String[] args) {
????MyThread thread = new MyThread();
????Thread t1 = new Thread(thread?? "Thread-1");
????Thread t2 = new Thread(thread?? "Thread-2");
????t1.start();
????t2.start();
????}
????}
??????????
????>>??????????Thread-1
????>>??????????Thread-2
????????ThreadLocal?????
????ThreadLocal?????????????????壬?????????????“???????”???????ThreadLocal?????????Thread??????Thread?????????????????????ThreadLocalVariable??????????????Щ???????ThreadLocal??????????ThreadLocal??????????????????????????????????????????????????????????????????????????????????????????????
???????????????????(ThreadLocal variables)??????
????????????????(ThreadLocal variables)?????????????????????????????????????????????????????????????????????????????????????????ThreadLocal????????????????е?(private static)??γ???????????У?????????????????????????????ThreadLocal???????????????ThreadLocal??????????????????
???????????????????????????????????????????????????????????ThreadLoacl????integer ?????????????????????????
????public class Test implements Runnable {
????private static ThreadLocal<Integer> num = new ThreadLocal<Integer>();
????public void run() {
????num.set(0);
????for (int i = 0; i < 3; i++) {
????num.set(num.get() + 1);
????System.out.println(Thread.currentThread().getName() + ":num="
????+ num.get());
????}
????}
????public static void main(String[] args) {
????Test test = new Test();
????Thread t1 = new Thread(test?? "Thread-1");
????Thread t2 = new Thread(test?? "Thread-2");
????Thread t3 = new Thread(test?? "Thread-3");
????t1.start();
????t2.start();
????t3.start();
????}
????}
???????н?????£?
????Thread-3:num=1
????Thread-2:num=1
????Thread-1:num=1
????Thread-2:num=2
????Thread-3:num=2
????Thread-2:num=3
????Thread-1:num=2
????Thread-1:num=3
????Thread-3:num=3
???????????????????????г????????????????ThreadLocal???????
???????????????DAO??????????????????????????(connection)??????????????????????????
????????????????session???????????????ThreadLocal<Session> session =     new ThreadLocal<Session>();
????ThreadLoacal?????????????
????1????????????У???????????????????????????????????????????????????????????????????????????????????????????????ж?д?????????????????????????????????????????????????????д????????
????2????ThreadLocal??????????????????????????????ThreadLocal???????????????????????????????????????????????????????????????????????????????????????????б??????????????????ThreadLocal????????????????????д??????????????????????????????ThreadLocal??
????3?????????????????????????????????????????????“????任???”????????ThreadLocal??????“???任???”?????????????????????ò?????????????????????????????????????????????????????????????
????????InvalidMonitorStateException??
????????wait()/notify()/notifyAll()?е??κ??????????????????????л???????????????????IllegalMonitorStateException????(???????????????ж?????κ?????????????????????????????wait()/notify()/notifyAll()?)???????????RuntimeExcpetion???????????????????????(???????????????????).???RuntimeException??????????????wait()??notify()??notifyAll()????????????
???????′?????????????????????????ж?????????????????
????public class Common implements Runnable {
????public synchronized void method1() throws InterruptedException {
????Thread.sleep(1000);
????System.out.println("Method 1 called");
????Thread.sleep(1000);
????System.out.println("Method 1 done");
????}
????public synchronized void method2() throws InterruptedException {
????Thread.sleep(1000);
????System.err.println("Method 2 called");
????Thread.sleep(1000);
????System.err.println("Method 2 done");
????}
????public void run() {
????System.out.println("Running " + Thread.currentThread().getName());
????try {
????if (Thread.currentThread().getName().equals("Thread-1")) {
????this.wait(1000);
????method1();
????} else {
????method2();
????notifyAll();
????}
????} catch (Exception e) {
????e.printStackTrace();
????}
????}
????public static void main(String[] args) throws InterruptedException {
????Common c = new Common();
????Thread t1 = new Thread(c?? "Thread-1");
????Thread t2 = new Thread(c?? "Thread-2");
????t1.start();
????t2.start();
????}
????}
?????????′???????????
????public class Common implements Runnable {
????public synchronized void method1() throws InterruptedException {
????Thread.sleep(1000);
????System.out.println("Method 1 called");
????Thread.sleep(1000);
????System.out.println("Method 1 done");
????}
????public synchronized void method2() throws InterruptedException {
????Thread.sleep(1000);
????System.err.println("Method 2 called");
????Thread.sleep(1000);
????System.err.println("Method 2 done");
????}
????public void run() {
????System.out.println("Running " + Thread.currentThread().getName());
????try {
????if (Thread.currentThread().getName().equals("Thread-1")) {
????synchronized(this){
????this.wait(1000);
????}
????method1();
????} else {
????method2();
????synchronized (this) {
????notifyAll();
????}
????}
????} catch (Exception e) {
????e.printStackTrace();
????}
????}
????public static void main(String[] args) throws InterruptedException {
????Common c = new Common();
????Thread t1 = new Thread(c?? "Thread-1");
????Thread t2 = new Thread(c?? "Thread-2");
????t1.start();
????t2.start();
????}
????}