控制三个线程依次对一个字符串末尾添加字母

2019-09-29 15:55:30 浏览数 (1)

题目描述:

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

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

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

运行结果:

代码:

代码语言:javascript复制
/**
 * 
 */
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();
                        }
                        
                    }
                 }
            }
            
            
        }
    }


}

0 人点赞