zoukankan      html  css  js  c++  java
  • java多线程笔试题---所有线程func1()执行完了,再执行func2()的实现

    1.问题描述:

      某个类有2个方法func1(),func2(),有threadCount个线程,要求所有线程执行完func1(),再执行func2()。

    2.分析:

      1)所有线程的func1()执行完的标准是什么?

      2)线程之间怎样共享变量?

    3.代码实现

      1)源代码:

    /**
     * 
     */
    package com.sunny.www.interview;
    
    import java.util.concurrent.atomic.AtomicInteger;
    
    /**
     * 所有线程执行完方法1,再执行方法2
     * @author sunny
     *
     */
    public class Syn2Threads implements Runnable {
        private AtomicInteger threadCount;        
        
        public Syn2Threads(AtomicInteger threadCount) {
            super();
            this.threadCount = threadCount;
        }
        public AtomicInteger getThreadCount() {
            return threadCount;
        }
        public void setThreadCount(AtomicInteger threadCount) {
            this.threadCount = threadCount;
        }
        private void func1(){
            System.out.println(Thread.currentThread().getName() + " execute func1,threadCount=" + threadCount.decrementAndGet());
            
        }
        private void func2(){
            System.out.println(Thread.currentThread().getName() + " execute func2,threadCount=" + threadCount);
        }
        
        @Override
        public void run() {
            func1();
            synchronized(threadCount){
                while (0 < threadCount.get())  {
                    try {
                        System.out.println(Thread.currentThread().getName() + " func1 not finished totally,threadCount=" + threadCount);
                        threadCount.wait();                    
                    } catch (InterruptedException e) {
                        System.out.println(Thread.currentThread().getName() + " wait error");
                    }
                }
                threadCount.notifyAll();
            }
            func2();
        }
    
        public static void main(String[] args) {
            int threadCount = 5;
            Syn2Threads syn2Threads = new Syn2Threads(new AtomicInteger(threadCount));
            for(int i = 1; i <= threadCount; i++){
                new Thread(syn2Threads,"thread-" + i).start();
            }
        }
    
    }

      2)运行效果:

    thread-2 execute func1,threadCount=4
    thread-2 func1 not finished totally,threadCount=3
    thread-1 execute func1,threadCount=3
    thread-4 execute func1,threadCount=2
    thread-4 func1 not finished totally,threadCount=2
    thread-1 func1 not finished totally,threadCount=2
    thread-5 execute func1,threadCount=1
    thread-5 func1 not finished totally,threadCount=1
    thread-3 execute func1,threadCount=0
    thread-3 execute func2,threadCount=0
    thread-5 execute func2,threadCount=0
    thread-1 execute func2,threadCount=0
    thread-4 execute func2,threadCount=0
    thread-2 execute func2,threadCount=0
  • 相关阅读:
    项目乱码导致原因
    springmvc配置文件
    IntelliJ IDEA启动Tomcat后,却无法访问Tomcat主页 等一系列问题
    java错题
    java 从键盘录入的三种方法
    java内部类 和外部类的区别
    多线程
    24中模式详解
    java的强制类型转换
    正则表达式
  • 原文地址:https://www.cnblogs.com/sunny08/p/4882290.html
Copyright © 2011-2022 走看看