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();
                            }
                            
                        }
                     }
                }
                
                
            }
        }
    
    
    }
  • 相关阅读:
    HDU 5363 Key Set(快速幂取模)
    HDU 5339 Untitled(暴搜)
    POJ 2406 Power Strings
    Dedecms备份还原网站有效方法
    DEDECMS文章列表每隔8行文章添加分隔虚线
    DEDECMS突破TAG和关键字长度的限制
    为织梦dedecms制作全文RSS订阅源
    DedeCms中Channel用typeid无效
    织梦CMS/Dedecms添加自定义函数
    sql批量换dedecms文章来源和作者
  • 原文地址:https://www.cnblogs.com/CherishTheYouth/p/CherishTheYouth_20190918.html
Copyright © 2011-2022 走看看