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
  • 相关阅读:
    安装SQLServer2000提示“无法验证产品密钥”的解决方法
    【转】SQL Server数据库开发的二十一条军规
    Asp.Net实现FORM认证的一些使用技巧
    记住window.open的用法
    VS2005的关于母版页嵌套的一个小技巧
    一种写法的区别
    问个关于VS使用上的问题
    Server.MapPath方法的应用方法
    一个关于重定向的问题研究,应该具有实用性
    象WORD一样,双击.doc的文件就自动打开WORD并编辑该文件(转)
  • 原文地址:https://www.cnblogs.com/sunny08/p/4882290.html
Copyright © 2011-2022 走看看