一、创建线程的方式

package cn.itcast.heima2;

public class TraditionalThread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Thread thread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                    System.out.println(this.getName());
                }
            }
        };
        thread.start();

        Thread thread2 = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                }
            }
        });
        thread2.start();//更可以体现面向对象的思想,线程和代码隔离
        
        
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("runnable:"+Thread.currentThread().getName());
                }
            }
        }){
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("thread:"+Thread.currentThread().getName());
                }
            }
        }.start();
    }

}

二、定时器

代码1:

package cn.itcast.heima2;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class TraditionalTimerTest {
    public static void main(String[] args) {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("OUTER:boom!");
            }
        },10000,3000);
        
        while(true){
            System.out.println(Calendar.getInstance().get(Calendar.SECOND));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

代码2:

package cn.itcast.heima2;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class TraditionalTimerTest {
    
    static class MyTimerTask extends TimerTask{
        static int count=0;
        @Override
        public void run() {
            // TODO Auto-generated method stub
            count=(count+1)%2;
            System.out.println("OUTER:boom!");
            new Timer().schedule(new MyTimerTask(), 2000+2000*count);
        }
        
    }
    
    public static void main(String[] args) {
        
        
        
        new Timer().schedule(new MyTimerTask(),2000);
        
        while(true){
            System.out.println(Calendar.getInstance().get(Calendar.SECOND));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

三、线程同步通信技术

子线程10次,然后主线程100次,然后子线程10次,然后主线程100次。循环50次

package cn.itcast.heima2;

public class TraditionalThreadComunication {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Business business = new TraditionalThreadComunication().new Business();

        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < 50; i++) {
                    business.sub(i);
                }
            }

        }).start();
        

        for (int i = 0; i < 50; i++) {
            business.main(i);
        }
    }

    class Business {
        
        private boolean bShouldSub=true;
        
        public synchronized void sub(int i) {
            if(!bShouldSub){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            for (int j = 0; j < 10; j++) {
                System.out.println("sub thread sequence:" + j + " loop of " + i);
            }
            bShouldSub=false;
            this.notify();
            
        }

        public synchronized void main(int i) {
            if(bShouldSub){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            for (int j = 0; j < 100; j++) {
                System.out.println("main thread sequence:" + j + " loop of " + i);
            }
            bShouldSub=true;
            this.notify();
        }
        
    }

}

四、线程范围内共享变量的概念与作用

如下代码中会出现问题

package cn.itcast.heima2;

import java.util.HashMap;
import java.util.Random;

public class ThreadScopeShareData {

    private static int data = 0;

    private static HashMap<Thread, Integer> threadData = new HashMap<>();

    public static void main(String[] args) {

        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {

                 @Override
                public void run() {
                    // TODO Auto-generated method stub
                    data = new Random(System.currentTimeMillis()).nextInt();
                    System.out.println(Thread.currentThread().getName() + " has put data :" + data);
                    threadData.put(Thread.currentThread(), data);
                    new A().get();
                    new B().get();
                }

            }).start();

        }
    }

    static class A {
        public void get() {
            int data=threadData.get(Thread.currentThread());
            System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data);
        }
    }

    static class B {
        public void get() {
            int data=threadData.get(Thread.currentThread());
            System.out.println("B from " + Thread.currentThread().getName() + " has put data :" + data);
        }
    }

}

解决方法:

package cn.itcast.heima2;

import java.util.HashMap;
import java.util.Random;

public class ThreadScopeShareData {

    private static int data = 0;
    static long[] seed = new long[] { 12345612, 654321 };
    private static HashMap<Thread, Integer> threadData = new HashMap<>();

    public static void main(String[] args) {

        new Thread(new ThreadScopeShareData.MyRunnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                data = MyRunnable.rd.nextInt();
                System.out.println(Thread.currentThread().getName() + " has put data :" + data);
                 threadData.put(Thread.currentThread(), data);
                new A().get();
                new B().get();
            }

        }).start();

        new Thread(new ThreadScopeShareData.MyRunnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                data = MyRunnable.rd.nextInt();
                System.out.println(Thread.currentThread().getName() + " has put data :" + data);
                 threadData.put(Thread.currentThread(), data);
                new A().get();
                new B().get();
            }

        }).start();

    }

    static class A {
        public void get() {
             int data=threadData.get(Thread.currentThread());
            System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data);
        }
    }

    static class B {
        public void get() {
             int data=threadData.get(Thread.currentThread());
            System.out.println("B from " + Thread.currentThread().getName() + " has put data :" + data);
        }
    }

    abstract static class MyRunnable implements Runnable {
        static Random rd = new Random();

        public abstract void run();

    }

}

五、读写锁技术的妙用

一个线程在写的时候其他线程都不能打断写入过程,就是写入的时候不能读取

package cn.itcast.heima2;

import java.util.Random;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockTest {

    static Random rd = new Random();

    public static void main(String[] args) {
        final Queue3 q3 = new Queue3();
        for (int i = 0; i < 3; i++) {
            new Thread() {
                public void run() {
                    while (true) {
                        q3.get();
                    }
                }

            }.start();

            new Thread() {
                public void run() {
                    while (true) {
                        q3.put(rd.nextInt(10000));
                    }
                }

            }.start();
        }

    }
}

class Queue3 {
    private Object data = null;// 共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。
    ReadWriteLock rwl = new ReentrantReadWriteLock();

    public void get() {
        rwl.readLock().lock();
        try {
            System.out.println(Thread.currentThread().getName() + " be ready to read data!");
            Thread.sleep((long) (Math.random() * 1000));
            System.out.println(Thread.currentThread().getName() + " have read data :" + data);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rwl.readLock().unlock();
        }
    }

    public void put(Object data) {

        rwl.writeLock().lock();
        try {
            System.out.println(Thread.currentThread().getName() + " be ready to write data!");
            Thread.sleep((long) (Math.random() * 1000));
            this.data = data;
            System.out.println(Thread.currentThread().getName() + " have write data: " + data);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rwl.writeLock().unlock();
        }

    }
}
package cn.itcast.heima2;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class CacheDemo {

    private Map<String, Object> cache = new HashMap<String, Object>();
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    private ReadWriteLock rwl = new ReentrantReadWriteLock();
    public  Object getData(String key){
        rwl.readLock().lock();
        Object value = null;
        try{
            value = cache.get(key);
            if(value == null){
                rwl.readLock().unlock();
                rwl.writeLock().lock();//A
                try{
                    if(value==null){//如果有多个线程同时到达A点了,那这个数据要写入多次,这是不对的
                        value = "aaaa";//实际失去queryDB();
                    }
                }finally{
                    rwl.writeLock().unlock();
                }
                rwl.readLock().lock();
            }
        }finally{
            rwl.readLock().unlock();
        }
        return value;
    }
}

z_dominic
115 声望15 粉丝

你有freestyle吗?