zoukankan      html  css  js  c++  java
  • 多线程资源竞争 --交叉打印数字和字母

    package com.java.main.test.thread;
    
    public class TestThread {
        
        public static Object object = new Object();
        public static void main(String[] args) {
            //TestThread testThread = new TestThread();
            //new Thread(new CharThread(testThread)).start();
            //new Thread(new NumThread(testThread)).start();
            new Thread(new PrintChar()).start();
            new Thread(new PrintNum()).start();
        }
        
        /*public static void main(String[] args) {
            // TODO Auto-generated method stub
            ExecutorService service = Executors.newFixedThreadPool(2);
            service.execute(new Runnable() {
     
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    int i = 0;
                    for (; i < 26; i++) {
                        System.out.print(i);
                        Thread.yield();
                    }
                }
            });
            service.execute(new Runnable() {
                char[] cs = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                        'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                        'u', 'v', 'w', 'x', 'y', 'z' };
     
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    int i = 0;
                    for (; i < 26; i++) {
                        System.out.print(cs[i]);
                        Thread.yield();
                    }
                }
            });
     
        }
        */
        
        static class PrintChar implements Runnable{
            public void printChar(){
                for (int i = 'a'; i <= 'z'; i++) {
                    synchronized(object){
                        System.out.print((char)i);
                        object.notifyAll();
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
    
            @Override
            public void run() {
                printChar();
            }
        }
        
        static class PrintNum implements Runnable{
            public void printNum(){
                for (int i = 0; i <= 26; i++) {
                    synchronized(object){
                        System.out.print(i+1);
                        object.notifyAll();
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            
            @Override
            public void run() {
                printNum();
            }
        }
        
    
    
    }
  • 相关阅读:
    开端
    springboot打包失败
    CONCAT_WS函数
    关于使用|作为分隔符
    JSONArray数组
    Math.ceil(double)向上取整
    $.unique(array)数组去重
    觉得没有问题,却始终没有按照预想的走的问题
    关于mouseover与mouseout以及mouseleave和mouseenter
    关于网页元素定义click事件,点击一次触发两次问题解决办法
  • 原文地址:https://www.cnblogs.com/phyxis/p/5824725.html
Copyright © 2011-2022 走看看