zoukankan      html  css  js  c++  java
  • 控制三个线程依次对一个字符串末尾添加字母

    题目描述:

    控制三个线程,按顺序在“Hello”字符串后添加“_A”,“_B”,"_C" ,输出 “Hello_A_B_C”;

    实现方法:使用wait()和notifyAll()方法实现;

    核心思想:在A线程运行完成时,同时设置B线程执行的条件,并唤醒(使用notifyAll())其他所有阻塞的线程,当A线程执行完后,如果获得CPU时间片的线程是B线程,则执行,如果不是,则使用wait()方法让该线程挂起。这样就可以保证线程执行的顺序。

    运行结果:

    代码:

    /**
     * 
     */
    package com.cherish.createThread;
    
    import java.util.Scanner;
    import java.util.concurrent.Callable;
    import java.util.concurrent.Executor;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * @author CherishTheYouth
     *
     */
    public class TongCheng{
        
        
        public static void main(String[] args)
        {
            TongCheng tc = new TongCheng();
            HelloABC hello = tc.new HelloABC("hello");
            ExecutorService es = Executors.newFixedThreadPool(3);
            es.execute(tc.new PrintThread(hello,"A") );
            es.execute(tc.new PrintThread(hello,"B") );
            es.execute(tc.new PrintThread(hello,"C") );
            es.shutdown();
                    
        }
        
        public class HelloABC{
            private String str = null;
            private String nextStr = "A"; //用于控制线程调用的顺序
            
            public HelloABC(String str)
            {
                this.str = str;
            }
            
            public void print()
            {
                
                System.out.println(str);
            }
            
    
            public String getString()
            {
                return str;
            }
            
            public void setString(String s)
            {
                str = new StringBuffer(str).append("_").append(s).toString();
            }
            
            public void setNextString(String current)
            {
                switch(current) {
                case "A":
                    nextStr = "B";
                    break;
                case "B":
                    nextStr = "C";
                    break;
                case "C" :
                    nextStr = "A";
                    break;
                }
            }
        }
        
        public class PrintThread implements Runnable{
            private HelloABC hello = null;
            private String letter;
            
            public PrintThread(HelloABC hello,String letter)
            {
                this.hello = hello;
                this.letter = letter;
            }
            
            @Override
            public void run() {
                // TODO 自动生成的方法存根
                for(int i = 0;i< 3;i++)
                {
                    synchronized (hello) {
                        if(hello.nextStr.equals(letter))
                        {
                            hello.setString(letter);
                            hello.setNextString(letter);
                            hello.print();
                            hello.notifyAll();
            
                        }else
                        {
                            try {
                                hello.wait();
                            } catch (InterruptedException e) {
                                // TODO: handle exception
                                e.printStackTrace();
                            }
                            
                        }
                     }
                }
                
                
            }
        }
    
    
    }
  • 相关阅读:
    C++函数参数传参的本质解析
    C#值类型和引用类型详解
    C#学习笔记(转换)
    C#学习笔记(泛型)
    # Java反射2——获取实体所有属性和方法,并对属性赋值
    Java反射1——扫描某个包下的所有类
    JSR教程2——Spring MVC数据校验与国际化
    JSR教程1——JSR 303
    Github如何撤销提交并清除痕迹
    论文第5章:Android绘图平台的实现
  • 原文地址:https://www.cnblogs.com/CherishTheYouth/p/CherishTheYouth_20190918.html
Copyright © 2011-2022 走看看