프로세스/다중 스레드

8526 단어
프로세스와 스레드

라인
           
      Thread 
        MyThread thread = new MyThread();
        thread.start();//    
      Runnable  
        MyRunnable runnable = new MyRunnable();
        new Thread(runnable).start();   
API
    Thread    
        getName()
                     
        currentThread()
                  
        sleep()
                        
              ?
      ,     。
            ?
               。

타이머 생성
   
    1.Timer  TimerTask 

//5         
//     
//            ,     
Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        //  
    }
}, 5*1000);

//5         ,   2      
Timer timer = new Timer();
//5       
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        //  
    }
}, 5*1000,2*1000);  

스레드 동기화synchronized
1.     
    synchronized (this) {
        
    }
    
2.    
    public synchronized void output(){}
          Java        Test.class

스레드 통신 -wait/notify
       

Lock&Condition
Lock         synchronized        ,        。
           。                     ,    
    Lock  ,                  ,          。

스레드 범위 내의 공유 데이터
//   ,       
    public class ThreadShareData1 {
        private static int data = 0;
        static class A{
            public void get(){
                System.out.println(Thread.currentThread().getName()+" a:"+data);
            }
        }
        static class B{
            public void get(){
                System.out.println(Thread.currentThread().getName()+" b:"+data);
            }
        }
        public static void main(String[] args) {
            for(int i = 0;i<5;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        data = new Random().nextInt(100);
                        System.out.println(Thread.currentThread().getName()+" input data :"+data);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    }
//       ,      
    public class ThreadShareData {
        private static Map threadData = new HashMap();
        static class A{
            public void get(){
                int data = threadData.get(Thread.currentThread());
                System.out.println(Thread.currentThread().getName()+" a:"+data);
            }
        }
        static class B{
            public void get(){
                int data = threadData.get(Thread.currentThread());
                System.out.println(Thread.currentThread().getName()+" b:"+data);
            }
        }
        public static void main(String[] args) {
            for(int i = 0;i<5;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int data = new Random().nextInt(100);
                        System.out.println(Thread.currentThread().getName()+" input data :"+data);
                        threadData.put(Thread.currentThread(), data);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    }
//Java    ,          
//       ,      
//  :  ThreadLocal        ,           。        ,      ThreadLocal  。
//                。
    public class ThreadShareData {
        //    map    
        private static ThreadLocal tl = new ThreadLocal();
        static class A{
            public void get(){
                int data = tl.get();
                System.out.println(Thread.currentThread().getName()+" a:"+data);
            }
        }
        static class B{
            public void get(){
                int data = tl.get();
                System.out.println(Thread.currentThread().getName()+" b:"+data);
            }
        }
        public static void main(String[] args) {
            for(int i = 0;i<5;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int data = new Random().nextInt(100);
                        System.out.println(Thread.currentThread().getName()+" input data :"+data);
                        tl.set(data);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    }
//       ,      ,      
    class User{
        private int id;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    public class ThreadShareData {
        //    map    
        private static ThreadLocal tl = new ThreadLocal();
        static class A{
            public void get(){
                User user = tl.get();
                System.out.println(Thread.currentThread().getName()+" a:"+user.getName());
            }
        }
        static class B{
            public void get(){
                User user = tl.get();
                System.out.println(Thread.currentThread().getName()+" b:"+user.getName());
            }
        }
        public static void main(String[] args) {
            for(int i = 0;i<2;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int data = new Random().nextInt(100);
                        System.out.println(Thread.currentThread().getName()+" input data :"+data);
                        User user = new User();
                        user.setId(data);
                        user.setName("user"+data);
                        tl.set(user);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    }
    
    public class Data {
        private Data(){}
        /**
         *          ThreadLocal   set  ,         map         。
         * key        ,value     。
         *             ThreadLocal clear  ,          。      ,            。
         * */
        private static ThreadLocal map = new ThreadLocal();
        public static Data getInstance(){//     synchronized
            Data data = map.get();
            if(data == null){
                data = new Data();
                map.set(data);
            }
            return data;
        }
        private int id;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    public class ThreadShareData {
        //    map    
        static class A{
            public void get(){
                System.out.println(Thread.currentThread().getName()+" a:"+Data.getInstance().getId());
            }
        }
        static class B{
            public void get(){
                System.out.println(Thread.currentThread().getName()+" b:"+Data.getInstance().getId());
            }
        }
        public static void main(String[] args) {
            for(int i = 0;i<2;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int data = new Random().nextInt(100);
                        System.out.println(Thread.currentThread().getName()+" input data :"+data);
                        Data.getInstance().setId(data);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    }
                
           Runnable  ,             

좋은 웹페이지 즐겨찾기