`
Donald_Draper
  • 浏览: 998403 次
社区版块
存档分类
最新评论

CountDownLatch使用场景

    博客分类:
  • JUC
阅读更多
Java 理论与实践: 正确使用 Volatile 变量:http://www.ibm.com/developerworks/cn/java/j-jtp06197.html
聊聊并发(一)——深入分析Volatile的实现原理:http://www.infoq.com/cn/articles/ftf-java-volatile
深入理解Java内存模型(四)——volatile:http://www.infoq.com/cn/articles/java-memory-model-4/
Java的多线程机制系列:(四)不得不提的volatile及指令重排序(happen-before):
http://www.cnblogs.com/mengheng/p/3495379.html

CountDownLatch同步化的一个辅助工具,允许一个或多个线程等待,直到所有线程中执行完成
比如主线程计算一个复杂的计算表达式,将表达式分为多个子表达式在线程中去计算,
主线程要计算表达式的最后值,必须等所有的线程计算完子表达式计算,方可计算表达式的值;
再比如一个团队赛跑游戏,最后要计算团队赛跑的成绩,主线程计算最后成绩,要等到所有
团队成员跑完,方可计算总成绩。使用情况两种:第一种,所有线程等待一个开始信息号,当开始信息号启动时,所有线程执行,等待所有线程执行完;第二种,所有线程放在线程池中,执行,等待所有线程执行完,方可执行主线程任务方可执行主线程任务。

测试有统一开始信号的情况:
package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class RunnerGames {
	public static void main(String[] args) {
		   CountDownLatch startSignal = new CountDownLatch(1);
		   CountDownLatch doneSignal = new CountDownLatch(3);
		   ExecutorService  exec = Executors.newCachedThreadPool();
		   RunnableMan rm1 = new RunnableMan(startSignal,doneSignal, 1000);
		   RunnableMan rm2 = new RunnableMan(startSignal,doneSignal, 2000);
		   RunnableMan rm3 = new RunnableMan(startSignal,doneSignal, 3000);
		   Future<Integer> score1 = exec.submit(rm1);
		   Future<Integer> score2 = exec.submit(rm2);
		   Future<Integer> score3 = exec.submit(rm3);
		   System.out.println("开始赛跑......");
		   startSignal.countDown();
		   try {
			   doneSignal.await();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}  
		   int sumScores =0;
		   try {
			   try {
				sumScores  = score1.get()+score2.get()+score3.get();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		   System.out.println("团队赛跑结束,最后成绩为:"+sumScores);
		   exec.shutdown();
		 }
}


package juc;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

class RunnableMan implements Callable<Integer> {  
	  private final CountDownLatch startSignal;
	  private final CountDownLatch doneSignal;             
	  private final int i;                                 
	  RunnableMan(CountDownLatch startSignal,CountDownLatch doneSignal, int i) {   
		 this.startSignal = startSignal;
	     this.doneSignal = doneSignal;                     
	     this.i = i;                                       
	  }                                                    
	  public Integer call() {        
	     try {
	    	startSignal.await();
			doRun(i);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}                                      
	     doneSignal.countDown();
	     return new Integer(i);
	  }                                                    
	  void doRun(int i) throws InterruptedException { 
		  System.out.println("选手"+i/1000+"正在赛跑中........");
		  Thread.sleep(i*2);
	  }                             
}
                                                     

测试结果:
开始赛跑......
选手3正在赛跑中........
选手2正在赛跑中........
选手1正在赛跑中........
团队赛跑结束,最后成绩为:6000


测试第二种情况,没有开始信号
package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**AtomicInteger,一个提供原子操作的Integer的类。
在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,
不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。*/
public class ShopTickets {
	private static volatile AtomicInteger tickets = new AtomicInteger(10);
	public static void main(String[] args) throws InterruptedException {
		  CountDownLatch doneSignal = new CountDownLatch(3);
		   Executor exec = Executors.newCachedThreadPool();
		   exec.execute(new TicketSales(doneSignal,"售票员1",tickets));
		   exec.execute(new TicketSales(doneSignal,"售票员2",tickets));
		   exec.execute(new TicketSales(doneSignal,"售票员3",tickets));
		   doneSignal.await();  
		   System.out.println("票已售完,所有售票员,停止售票");
		 }
}


package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

class TicketSales implements Runnable {  
	  private final CountDownLatch doneSignal;   
	  private final AtomicInteger tickets;
	  private String saleName;
	  
	  TicketSales(CountDownLatch doneSignal, String saleName,AtomicInteger tickets) { 
	     this.doneSignal = doneSignal;                   
	     this.saleName = saleName; 
	     this.tickets = tickets;
	  }         
	  public  void run() {                                
		  doSales(tickets);                                    
	  }                                                  
	  public  void  doSales(AtomicInteger tickets) { 
		   try {
				   while(tickets.get()>0){
					System.out.println(saleName+"卖完一张票,还有:"+tickets.decrementAndGet()+"张");
					Thread.sleep(1000);
			   }
				doneSignal.countDown();
		  }catch (InterruptedException e) {
			e.printStackTrace();
		}
	  }                              
}                             
 
                     
测试结果:                                                                                                                                      
售票员1卖完一张票,还有:8张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:9张
售票员3卖完一张票,还有:5张
售票员1卖完一张票,还有:6张
售票员3卖完一张票,还有:3张
售票员2卖完一张票,还有:4张
售票员3卖完一张票,还有:1张
售票员1卖完一张票,还有:2张
售票员2卖完一张票,还有:0张
票已售完,所有售票员,停止售票
有时测试结果为:

售票员1卖完一张票,还有:9张
售票员2卖完一张票,还有:8张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:6张
售票员1卖完一张票,还有:5张
售票员3卖完一张票,还有:4张
售票员3卖完一张票,还有:2张
售票员2卖完一张票,还有:1张
售票员1卖完一张票,还有:3张
售票员1卖完一张票,还有:0张
售票员2卖完一张票,还有:-1张
票已售完,所有售票员,停止售票

把Thread.sleep(1000)这句去掉,票数为负数的概率较小,但还是会出现,
我测试了3分钟,出现2次。
关注这一句:
while(tickets.get()>0){
问题可能在tickets.get()的时候,票数不为零,在卖票的时候,其他线程已经在其基础上,修改
tickets,而当前线程tickets.get()已经做过判断,不为零,导致的错误。
再次修改:
package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class ShopTickets2 {
	/**
	 * AtomicInteger,一个提供原子操作的Integer的类。 在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,
	 * 不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。
	 */
	private static volatile AtomicInteger tickets = new AtomicInteger(10);
	public static void main(String[] args) throws InterruptedException {
		CountDownLatch doneSignal = new CountDownLatch(3);
		Executor exec = Executors.newCachedThreadPool();
		exec.execute(new TicketSales2(doneSignal, "售票员1", tickets));
		exec.execute(new TicketSales2(doneSignal, "售票员2", tickets));
		exec.execute(new TicketSales2(doneSignal, "售票员3", tickets));
		doneSignal.await();
		System.out.println("票已售完,所有售票员,停止售票");
	}
}


package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

class TicketSales2 implements Runnable { 
	  private static volatile AtomicBoolean isDoned = new AtomicBoolean(Boolean.TRUE);
	  private final CountDownLatch doneSignal;   
	  private final AtomicInteger tickets;
	  private String saleName;
	  
	  TicketSales2(CountDownLatch doneSignal, String saleName,AtomicInteger tickets) { 
	     this.doneSignal = doneSignal;                   
	     this.saleName = saleName; 
	     this.tickets = tickets;
	  }         
	  public  void run() {                                
		  doSales(tickets);                                    
	  }                                                  
	  public  void  doSales(AtomicInteger tickets) { 
		 while(isDoned.get()){
				System.out.println(saleName+"卖完一张票,还有:"+tickets.decrementAndGet()+"张");
				if(tickets.get()==0){
					isDoned.compareAndSet(true, false);
				}
            }
		doneSignal.countDown();
	  }                              
}     
                                              
                                                    
测试:
售票员1卖完一张票,还有:9张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:8张
售票员3卖完一张票,还有:5张
售票员1卖完一张票,还有:6张
售票员3卖完一张票,还有:3张
售票员2卖完一张票,还有:4张
售票员2卖完一张票,还有:0张
售票员3卖完一张票,还有:1张
售票员1卖完一张票,还有:2张
票已售完,所有售票员,停止售票
不在出现上述票数为负的情况,关键是添加了所有线程都可见,且线程安全的余票状态
private static volatile AtomicBoolean isDoned = new AtomicBoolean(Boolean.TRUE);


下面来看使用同步锁的场景:
package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class ShopTickets3 {
	/**
	 * AtomicInteger,一个提供原子操作的Integer的类。 在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,
	 * 不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。
	 */
	private static volatile AtomicInteger tickets = new AtomicInteger(10);
	private static volatile Object saleLock = new Object();
	public static void main(String[] args) throws InterruptedException {
		CountDownLatch doneSignal = new CountDownLatch(3);
		Executor exec = Executors.newCachedThreadPool();
		exec.execute(new TicketSales3(doneSignal, "售票员1", tickets,saleLock));
		exec.execute(new TicketSales3(doneSignal, "售票员2", tickets,saleLock));
		exec.execute(new TicketSales3(doneSignal, "售票员3", tickets,saleLock));
		doneSignal.await();
		System.out.println("票已售完,所有售票员,停止售票");
	}
}

package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

class TicketSales3 implements Runnable {  
	  private final CountDownLatch doneSignal;   
	  private final AtomicInteger tickets;
	  private final Object saleLock;
	  private String saleName;
	  
	  TicketSales3(CountDownLatch doneSignal, String saleName,AtomicInteger tickets, Object saleLock) { 
	     this.doneSignal = doneSignal;                   
	     this.saleName = saleName; 
	     this.tickets = tickets;
	     this.saleLock = saleLock;
	  }         
	  public  void run() {                                
		  doSales(tickets);                                    
	  }                                                  
	  public  void  doSales(AtomicInteger tickets) { 
		 boolean flag = true;
		 while(flag){
			 synchronized(saleLock){
				   if(tickets.get()>0){
					   System.out.println(saleName+"卖完一张票,还有:"+tickets.decrementAndGet()+"张");
					   saleLock.notifyAll();
				   }
				   else{
					   flag= false;
				   }
			   }
            }
		doneSignal.countDown();
	  }                              
}                                                    
                                  
控制台输出:
售票员1卖完一张票,还有:9张
售票员1卖完一张票,还有:8张
售票员1卖完一张票,还有:7张
售票员1卖完一张票,还有:6张
售票员1卖完一张票,还有:5张
售票员1卖完一张票,还有:4张
售票员1卖完一张票,还有:3张
售票员1卖完一张票,还有:2张
售票员3卖完一张票,还有:1张
售票员3卖完一张票,还有:0张
票已售完,所有售票员,停止售票
虽然也可以控制卖票,但只有两个售票员在售票,不能有效的利用资源,达到最大性能。
从上面可以看出,最有效方式为第二种。
/*
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Written by Doug Lea with assistance from members of JCP JSR-166
 * Expert Group and released to the public domain, as explained at
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

package java.util.concurrent;
import java.util.concurrent.locks.*;
import java.util.concurrent.atomic.*;

/**
 * A synchronization aid that allows one or more threads to wait until
 * a set of operations being performed in other threads completes.
 * 同步化的一个辅助工具,允许一个或多个线程等待,直到所有线程中执行完成
 *比如主线程计算一个复杂的计算表达式,将表达式分为多个子表达式在线程中去计算,
 *主线程要计算表达式的最后值,必须等所有的线程计算完子表达式计算,方可计算表达式的值;
 *再比如一个团队赛跑游戏,最后要计算团队赛跑的成绩,主线程计算最后成绩,要等到所有
 *团队成员跑完,方可计算总成绩。
 * <p>A {@code CountDownLatch} is initialized with a given [i]count[/i].
 * The {@link #await await} methods block until the current count reaches
 * zero due to invocations of the {@link #countDown} method, after which
 * all waiting threads are released and any subsequent invocations of
 * {@link #await await} return immediately.  This is a one-shot phenomenon
 * -- the count cannot be reset.  If you need a version that resets the
 * count, consider using a {@link CyclicBarrier}.
 *CountDownLatch有一个初始值,调用await的线程,要等待CountDownLatch值为0时,方可
 *往下执行
 * <p>A {@code CountDownLatch} is a versatile synchronization tool
 * and can be used for a number of purposes.  A
 * {@code CountDownLatch} initialized with a count of one serves as a
 * simple on/off latch, or gate: all threads invoking {@link #await await}
 * wait at the gate until it is opened by a thread invoking {@link
 * #countDown}.  A {@code CountDownLatch} initialized to [i]N[/i]
 * can be used to make one thread wait until [i]N[/i] threads have
 * completed some action, or some action has been completed N times.
 *
 * <p>A useful property of a {@code CountDownLatch} is that it
 * doesn't require that threads calling {@code countDown} wait for
 * the count to reach zero before proceeding, it simply prevents any
 * thread from proceeding past an {@link #await await} until all
 * threads could pass.
 *
 * <p><b>Sample usage:</b> Here is a pair of classes in which a group
 * of worker threads use two countdown latches:
 * [list]
 * <li>The first is a start signal that prevents any worker from proceeding
 * until the driver is ready for them to proceed;
 * <li>The second is a completion signal that allows the driver to wait
 * until all workers have completed.
 * [/list]
 *第一种,所有线程等待一个开始信息号,当开始信息号启动时,所有线程执行,等待所有线程执行完
 *方可执行主线程任务
 * <pre>
 * class Driver { // ...
 *   void main() throws InterruptedException {
 *     CountDownLatch startSignal = new CountDownLatch(1);
 *     CountDownLatch doneSignal = new CountDownLatch(N);
 *
 *     for (int i = 0; i < N; ++i) // create and start threads
 *       new Thread(new Worker(startSignal, doneSignal)).start();
 *
 *     doSomethingElse();  // don't let run yet
       //给所有线程一个开始信号,比如上面的团队赛发令枪
 *     startSignal.countDown();      // let all threads proceed
 *     doSomethingElse();
       //等待所有团队赛跑选手,释放跑完信号
 *     doneSignal.await();           // wait for all to finish
 *   }
 * }
 *
 * class Worker implements Runnable {
 *   private final CountDownLatch startSignal;
 *   private final CountDownLatch doneSignal;
 *   Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
 *      this.startSignal = startSignal;
 *      this.doneSignal = doneSignal;
 *   }
 *   public void run() {
 *      try {
         //等待开始信息号
 *        startSignal.await();
         //赛跑
 *        doWork();
          //跑完,计时员收到跑完信号通知
 *        doneSignal.countDown();
 *      } catch (InterruptedException ex) {} // return;
 *   }
 *
 *   void doWork() { ... }
 * }
 *
 * </pre>
 *
 * <p>Another typical usage would be to divide a problem into N parts,
 * describe each part with a Runnable that executes that portion and
 * counts down on the latch, and queue all the Runnables to an
 * Executor.  When all sub-parts are complete, the coordinating thread
 * will be able to pass through await. (When threads must repeatedly
 * count down in this way, instead use a {@link CyclicBarrier}.)
 *所有线程放在线程池中,执行,等待所有线程执行完,方可执行主线程任务
 * <pre>
 * class Driver2 { // ...
 *   void main() throws InterruptedException {
 *     CountDownLatch doneSignal = new CountDownLatch(N);
 *     Executor e = ...
 *
 *     for (int i = 0; i < N; ++i) // create and start threads
 *       e.execute(new WorkerRunnable(doneSignal, i));
 *
 *     doneSignal.await();           // wait for all to finish
 *   }
 * }
 *
 * class WorkerRunnable implements Runnable {
 *   private final CountDownLatch doneSignal;
 *   private final int i;
 *   WorkerRunnable(CountDownLatch doneSignal, int i) {
 *      this.doneSignal = doneSignal;
 *      this.i = i;
 *   }
 *   public void run() {
 *      try {
 *        doWork(i);
 *        doneSignal.countDown();
 *      } catch (InterruptedException ex) {} // return;
 *   }
 *
 *   void doWork() { ... }
 * }
 *
 * </pre>
 *
 * <p>Memory consistency effects: Until the count reaches
 * zero, actions in a thread prior to calling
 * {@code countDown()}
 * [url=package-summary.html#MemoryVisibility]<i>happen-before</i>[/url]
 * actions following a successful return from a corresponding
 * {@code await()} in another thread.
 *
 * @since 1.5
 * @author Doug Lea
 */
public class CountDownLatch {
    /**
     * Synchronization control For CountDownLatch.
     * Uses AQS state to represent count.
     *用一个 AQS状态代表信号量数
     */
    private static final class Sync extends AbstractQueuedSynchronizer {
        private static final long serialVersionUID = 4982264981922014374L;

        Sync(int count) {
            setState(count);
        }

        int getCount() {
            return getState();
        }
        //获取信号量
        protected int tryAcquireShared(int acquires) {
            return (getState() == 0) ? 1 : -1;
        }
       //释放信号量
        protected boolean tryReleaseShared(int releases) {
            // Decrement count; signal when transition to zero
            for (;;) {
                int c = getState();
                if (c == 0)
                    return false;
                int nextc = c-1;
                if (compareAndSetState(c, nextc))
                    return nextc == 0;
            }
        }
    }

    private final Sync sync;

    /**
     * Constructs a {@code CountDownLatch} initialized with the given count.
     *
     * @param count the number of times {@link #countDown} must be invoked
     *        before threads can pass through {@link #await}
     * @throws IllegalArgumentException if {@code count} is negative
     */
    public CountDownLatch(int count) {
        if (count < 0) throw new IllegalArgumentException("count < 0");
        this.sync = new Sync(count);
    }

    /**
     * Causes the current thread to wait until the latch has counted down to
     * zero, unless the thread is {@linkplain Thread#interrupt interrupted}.
     *
     * <p>If the current count is zero then this method returns immediately.
     *
     * <p>If the current count is greater than zero then the current
     * thread becomes disabled for thread scheduling purposes and lies
     * dormant until one of two things happen:
     * [list]
     * <li>The count reaches zero due to invocations of the
     * {@link #countDown} method; or
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
     * the current thread.
     * [/list]
     *
     * <p>If the current thread:
     * [list]
     * <li>has its interrupted status set on entry to this method; or
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting,
     * [/list]
     * then {@link InterruptedException} is thrown and the current thread's
     * interrupted status is cleared.
     *
     * @throws InterruptedException if the current thread is interrupted
     *         while waiting
     */
    public void await() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);
    }

    /**
     * Causes the current thread to wait until the latch has counted down to
     * zero, unless the thread is {@linkplain Thread#interrupt interrupted},
     * or the specified waiting time elapses.
     *
     * <p>If the current count is zero then this method returns immediately
     * with the value {@code true}.
     *
     * <p>If the current count is greater than zero then the current
     * thread becomes disabled for thread scheduling purposes and lies
     * dormant until one of three things happen:
     * [list]
     * <li>The count reaches zero due to invocations of the
     * {@link #countDown} method; or
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
     * the current thread; or
     * <li>The specified waiting time elapses.
     * [/list]
     *
     * <p>If the count reaches zero then the method returns with the
     * value {@code true}.
     *
     * <p>If the current thread:
     * [list]
     * <li>has its interrupted status set on entry to this method; or
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting,
     * [/list]
     * then {@link InterruptedException} is thrown and the current thread's
     * interrupted status is cleared.
     *
     * <p>If the specified waiting time elapses then the value {@code false}
     * is returned.  If the time is less than or equal to zero, the method
     * will not wait at all.
     *
     * @param timeout the maximum time to wait
     * @param unit the time unit of the {@code timeout} argument
     * @return {@code true} if the count reached zero and {@code false}
     *         if the waiting time elapsed before the count reached zero
     * @throws InterruptedException if the current thread is interrupted
     *         while waiting
     */
    public boolean await(long timeout, TimeUnit unit)
        throws InterruptedException {
        return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
    }

    /**
     * Decrements the count of the latch, releasing all waiting threads if
     * the count reaches zero.
     *
     * <p>If the current count is greater than zero then it is decremented.
     * If the new count is zero then all waiting threads are re-enabled for
     * thread scheduling purposes.
     *
     * <p>If the current count equals zero then nothing happens.
     */
    public void countDown() {
        sync.releaseShared(1);
    }

    /**
     * Returns the current count.
     *
     * <p>This method is typically used for debugging and testing purposes.
     *
     * @return the current count
     */
    public long getCount() {
        return sync.getCount();
    }

    /**
     * Returns a string identifying this latch, as well as its state.
     * The state, in brackets, includes the String {@code "Count ="}
     * followed by the current count.
     *
     * @return a string identifying this latch, as well as its state
     */
    public String toString() {
        return super.toString() + "[Count = " + sync.getCount() + "]";
    }
}

1
0
分享到:
评论
1 楼 di1984HIT 2017-07-31  
xuexile~~~ 

相关推荐

    【人工智能产业链分析】2018-2024年中国各地区AI产业集中度与财务状况及产业话语权研究

    中国全国AI人工智能产业链关系2018-2024集中度指标按地区分销售财务情况产业话语权指标等 数据来源:基于国内各部委公布数据及各地统计数据整理,或相关省、市数据 数据范围:全国、或各省份地市的统计数据 数据期间:(参见其文件名的标识表达了数据年度、或月度日度期间) 主要指标: 包含的数据文件有: 中国全国AI人工智能产业链产业基本情况2020-2024.zip 中国全国AI人工智能产业链产业上市公司按地区分销售情况统计表2020-2024.zip 中国全国AI人工智能产业链产业上市公司财务情况统计表2020-2024.zip 中国全国AI人工智能产业链产业上市公司销售情况统计表2020-2024.zip 中国全国AI人工智能产业链产业政策信息表2020-2024.zip 中国全国AI人工智能产业链关系表2024.zip 中国全国AI人工智能产业链集中度指标表2020-2024.zip 中国全国AI人工智能产业链名单2024.zip 中国全国AI人工智能产业链企业基本信息表2018-202501.zip 中国全国AI人工智能产业链企业名单2024.zip 中国全国AI人工智能产业链上市公司产业话语权指标表2020-2024.zip (近百MB数据的网盘链接)

    DOS游戏 - RPG - 侠客英雄传3

    注意事项: 1、运行游戏方法 :双击 “侠客英雄传3.bat” 运行。 2、更换光碟提示: 使用 CTRL+F4 组合键后 按空格即可跳过。

    电力电子领域两相交错并联Buck/Boost变换器的Matlab/Simulink仿真及控制策略分析

    内容概要:本文详细介绍了两相交错并联Buck/Boost变换器的建模与仿真,重点探讨了三种不同的控制方式及其性能表现。首先,文章描述了模型的基本架构,包括两个桥臂共用直流母线,MOSFET采用理想的双向开关,电流传感器安装在电感支路上。接着,分别讨论了开环控制、电压单环控制和电压电流双闭环控制的具体实现方法和优缺点。开环模式适用于观察硬件参数的影响,电压单环控制虽然稳定但在负载突变时响应较慢,而双闭环控制能够显著提高系统的动态响应和电流均流效果。此外,文章还分享了一些实用的仿真技巧,如正确设置死区时间和仿真步长,确保仿真的准确性和效率。 适合人群:电力电子工程师、科研人员、高校师生等对DC-DC变换器设计和仿真感兴趣的读者。 使用场景及目标:①研究两相交错并联Buck/Boost变换器的工作原理和控制策略;②优化变换器的设计参数,提高系统的稳定性和效率;③掌握Matlab/Simulink进行复杂电力电子系统仿真的方法和技术。 其他说明:文中提供了详细的代码片段和仿真波形,帮助读者更好地理解和应用相关理论和技术。

    ffmpeg7.0 + sdl3.0 播放音频

    ffmpeg7.0 + sdl3.0 播放音频

    基于龙贝格观测器的PMSM无传感器控制技术解析与应用

    内容概要:本文深入探讨了基于龙贝格观测器的永磁同步电机(PMSM)无传感器控制技术。首先介绍了龙贝格观测器相较于传统滑模观测器(SMO)的优势,特别是在减少系统抖振方面表现突出。接着详细解释了龙贝格观测器的工作原理,包括状态预测、误差补偿以及角度解算三大核心步骤,并提供了具体的代码实现。文中还讨论了实际工程应用中的挑战,如参数选择、噪声处理等问题,并给出了相应的解决方案。此外,文章通过实验数据展示了龙贝格观测器在不同工况下的性能优势,尤其是在高速和低速情况下的稳定性和响应速度。 适合人群:从事电机控制系统研究与开发的技术人员,尤其是关注无传感器控制领域的工程师。 使用场景及目标:适用于希望提升PMSM无传感器控制系统的稳定性、精确度的研发团队。主要目标是在保持高性能的同时降低系统复杂度,提高产品竞争力。 其他说明:文中不仅分享了理论知识和技术细节,还提供了大量实用的经验技巧,帮助读者更好地理解和应用龙贝格观测器进行实际项目开发。

    PMSM伺服系统转动惯量与阻尼系数在线辨识方法及仿真研究

    内容概要:本文深入探讨了永磁同步电机(PMSM)伺服系统的转动惯量和阻尼系数的在线辨识方法。文中介绍了两种主要的辨识方程:一种用于空载工况,另一种用于负载工况。通过详细的数学推导和Python、C、MATLAB代码示例,展示了如何在不同工况下精准辨识这些参数。此外,还讨论了1.5拍延时补偿、全电压前馈补偿和相电压重构等关键技术,以提高辨识精度和系统稳定性。仿真结果显示,在空载和负载突变情况下,辨识误差分别低于0.8%和2.3%。 适合人群:从事电机控制、自动化控制领域的研究人员和技术人员,尤其是对PMSM伺服系统感兴趣的工程师。 使用场景及目标:适用于需要在线辨识PMSM伺服系统转动惯量和阻尼系数的应用场合,如工业机器人、数控机床等。目标是在不影响正常生产的情况下,实时监测和调整电机参数,提升系统性能。 其他说明:本文不仅提供了理论推导和算法实现,还给出了具体的代码示例和仿真结果,便于读者理解和应用。同时,文中提到的技术可以作为其他类似算法验证的良好参考。

    (源码)基于Arduino的精确计时与PWM控制系统.zip

    # 基于Arduino的精确计时与PWM控制系统 ## 项目简介 本项目基于Arduino的TimerOne库,该库是Arduino平台上用于精确计时和PWM控制的开源库。主要面向Arduino板上的ATmega系列微控制器,可实现设置定时器、产生PWM信号、定时中断等功能,用于精确控制时间和电机速度。 ## 项目的主要特性和功能 1. 初始化定时器,设置初始参数。 2. 根据用户指定微秒数设置定时器周期。 3. 设定PWM输出的占空比,控制PWM输出大小。 4. 启动和停止PWM输出。 5. 设定和停止中断服务例行程序。 6. 重新启动和重置定时器。 7. 停止定时器计数。 8. 读取当前定时器的计数值并转换为微秒数。 ## 安装使用步骤 ### 安装 用户已下载项目源码文件后,可通过Arduino IDE的库管理器搜索并安装TimerOne库。 ### 使用 在代码中引入#include <TimerOne.h>,即可使用上述功能。

    weixin242基于微信小程序的外卖点餐系统设计与实现ssm(文档+源码)_kaic

    weixin242基于微信小程序的外卖点餐系统设计与实现ssm(文档+源码)_kaic

    (源码)基于Arduino的Wemos Mqtt Alarm Panel项目.zip

    # 基于Arduino的Wemos Mqtt Alarm Panel项目 ## 项目简介 本项目是一个基于Arduino平台的开源智能报警面板项目,命名为“Wemos Mqtt Alarm Panel”。该项目允许用户通过简单的MQTT操作来触发和控制报警系统。主要面向需要低成本、易于部署的智能家居或小型商业场所报警系统。项目仍在开发阶段,但已经具备基本功能并可供使用。 ## 项目的主要特性和功能 1. 低成本硬件需求主要使用Wemos D1 Mini或其他兼容的微控制器,以及Lolin 2.4英寸TFT显示屏。整体硬件成本较低,易于获取和部署。 2. 基于MQTT通信协议允许报警系统与MQTT服务器进行通信,实现远程控制和状态报告功能。 3. 界面友好采用直观的图形界面,支持触摸操作,方便用户进行交互。 4. 校准功能提供校准界面,确保触摸操作的准确性。 5. 可扩展性支持自定义报警事件和动作,允许用户根据需求进行个性化设置。 ## 安装使用步骤

    MATLAB实现SSA-ESN多输出回归:代码解析与应用实例

    内容概要:本文详细介绍了一个基于MATLAB的SSA-ESN(奇异谱分析-回声状态网络)多输出回归代码。该代码适用于处理复杂的非线性回归问题,具有多输出支持、友好的数据格式、丰富的可视化效果以及全面的评价指标等特点。文中不仅提供了详细的代码解析,还给出了具体的操作步骤和注意事项,帮助初学者快速理解和应用这一先进的回归方法。主要内容分为数据预处理、模型训练与预测、结果分析与可视化三个部分,涵盖了从数据准备到最终结果呈现的完整流程。 适合人群:对机器学习感兴趣特别是想学习和应用SSA-ESN进行多输出回归的新手程序员和研究人员。 使用场景及目标:①用于解决多输出的非线性回归问题;②提供一个完整的项目案例,帮助用户理解SSA-ESN的工作机制及其优势;③通过实际操作加深对机器学习理论的理解。 其他说明:代码已调试完毕,可以直接运行,附有详细的中文注释,便于学习和修改。此外,文中还提到了一些常见的错误及解决方案,如数据格式不匹配等问题。

    基于Matlab的模拟射击自动报靶系统:图像处理与计算机视觉技术实现高效报靶

    内容概要:本文详细介绍了一个基于Matlab的模拟射击自动报靶系统的实现方法。该系统利用图像处理技术和计算机视觉技术,通过一系列步骤如图像滤波、图像减影、二值化、噪声滤除、目标矫正、弹孔识别和环值判定,实现了对射击靶纸的自动化处理。此外,文中还介绍了如何使用Matlab的GUIDE工具创建友好的GUI界面,使系统更易于操作。系统不仅提高了报靶的速度和准确性,还在军事训练和民用射击活动中展现出广阔的应用前景。 适合人群:对图像处理、计算机视觉感兴趣的研发人员和技术爱好者,尤其是有一定Matlab基础的用户。 使用场景及目标:适用于射击训练和比赛中,用于快速准确地报靶,提高训练效率和比赛公平性。目标是通过自动化手段减少人工干预,确保报靶结果的客观性和实时性。 其他说明:文中提供了详细的代码示例和优化技巧,帮助读者更好地理解和实现该系统。此外,作者还分享了一些常见问题的解决方案,如光照突变、靶纸反光等问题的应对措施。

    【Docker Compose】高级编排实战指南:涵盖Web全栈、AI/ML、IoT、监控、CI/CD等多领域应用部署摘要 本文

    内容概要:本文深入探讨了 Docker Compose 的高级应用,旨在帮助用户从基础用户成长为能够驾驭复杂系统编排的专家。文章首先介绍了 Docker Compose 的核心高级特性,如 profiles、extends、depends_on、healthcheck、自定义网络、卷管理和环境变量管理。接着,通过 30 多个实战模板,覆盖了 Web 全栈、AI/ML、IoT、监控、CI/CD 等多个领域的复杂场景,展示了如何利用这些特性构建高效、可靠的应用环境。每个模板不仅提供了详细的代码示例,还附有解释要点,帮助读者理解其工作原理和应用场景。 适用人群:具备一定 Docker 基础,希望提升 Docker Compose 使用技能的开发者和运维人员,特别是那些需要管理复杂多服务应用的 DevOps 工程师。 使用场景及目标: 1. **Web 开发**:构建 LEMP、MERN 等全栈应用,简化开发和部署流程。 2. **数据处理**:实现 ETL 流程,结合消息队列和数据库进行高效数据处理。 3. **微服务架构**:使用 API 网关简化微服务入口管理,提升服务发现和路由配置的灵活性。 4. **监控与日志**:搭建 PLG 或 ELK 日志系统,实现日志的收集、存储和可视化。 5. **物联网**:构建 MQTT Broker 和时序数据库,支持 IoT 设备的数据接收和处理。 6. **机器学习**:部署 TensorFlow Serving 或 TorchServe,提供模型服务接口。 7. **CI/CD**:快速搭建 GitLab/Gitea 平台,支持代码托管和持续集成。 8. **安全测试**:使用 OWASP ZAP 对 Web 应用进行自动化或手动的安全扫描。 9. **教育与学习**:部署 Moodle,在线学习管理系统,支持课程创建和

    基于COMSOL的注浆技术仿真:浆液扩散特性与优化方案

    内容概要:本文详细探讨了利用COMSOL软件对注浆技术进行仿真的方法和技术细节。主要内容包括浆液扩散的数学建模、仿真模型的构建(如几何模型、边界条件、初始条件和控制方程)、关键参数(注浆压力、孔间距、地质条件)对浆液扩散的影响分析,以及实际工程应用案例。文中通过具体实例展示了如何通过仿真优化注浆施工参数,提高注浆效率并降低成本。此外,还讨论了倾斜裂隙、孔隙率和渗透率等因素对浆液扩散的具体影响及其应对措施。 适合人群:从事地下工程施工的技术人员、科研人员及高校相关专业师生。 使用场景及目标:①用于优化注浆施工方案,提高注浆效果;②为地下工程建设提供技术支持;③帮助研究人员深入理解浆液扩散机制。 其他说明:文章不仅提供了理论分析,还包括大量具体的代码示例和实践经验分享,有助于读者更好地理解和应用COMSOL仿真技术。

    【信息安全领域】实战项目汇总:涵盖网络渗透测试、Web应用安全加固、企业安全策略制定等多方面内容

    内容概要:本文列举了多个信息安全领域的实战项目示例,涵盖网络渗透测试、Web应用安全加固、企业安全策略制定与实施、恶意软件分析、数据泄露应急响应、物联网设备安全检测、区块链安全审计和云安全防护八大方面。每个项目均明确了具体的目标与步骤,如网络渗透测试通过模拟攻击发现并修复系统漏洞;Web应用安全加固则从代码审查、输入验证、身份验证、数据加密等方面确保应用安全;企业安全策略制定旨在构建全面的信息安全体系;恶意软件分析深入探究其功能与传播机制;数据泄露应急响应项目则聚焦于快速遏制影响、调查原因、恢复系统;物联网设备安全检测保障设备的安全性;区块链安全审计确保系统稳定可靠;云安全防护构建云环境下的安全体系。; 适合人群:信息安全从业人员、网络安全工程师、企业IT管理人员、安全研究人员。; 使用场景及目标:适用于希望深入了解信息安全各细分领域实战操作的专业人士,目标是掌握不同类型安全项目的实施流程与技术要点,提升实际工作中应对安全挑战的能力。; 其他说明:文中提供的项目示例不仅有助于理论学习,更为实际工作提供了具体的指导和参考,帮助相关人员在不同场景下有效开展信息安全工作,确保信息系统的安全性、稳定性和可靠性。

    基于matlab实现的博弈方法的电动汽车充电调度策略研究+源码(毕业设计&课程设计&项目开发)

    基于matlab实现的博弈方法的电动汽车充电调度策略研究+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用 基于matlab实现的博弈方法的电动汽车充电调度策略研究+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于matlab实现的博弈方法的电动汽车充电调度策略研究+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用 基于matlab实现的博弈方法的电动汽车充电调度策略研究+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用 基于matlab实现的博弈方法的电动汽车充电调度策略研究+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用 基于matlab实现的博弈方法的电动汽车充电调度策略研究+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用

    基于Matlab的违规限号车牌识别管理系统:从粗定位到精准识别

    内容概要:本文详细介绍了基于Matlab的违规限号车牌识别管理系统的开发过程和技术细节。系统主要分为多个步骤,包括车牌粗定位、灰度化、倾斜矫正、二值化、形态学处理、反色处理、精准定位、字符分割与识别、限号判断、语音播报和违规车牌信息导出。通过这些步骤,系统能够高效地识别并处理违规限号车辆,提升交通管理水平。 适用人群:适用于具有一定编程基础的技术人员,特别是对计算机视觉和数字图像处理感兴趣的开发者。 使用场景及目标:本系统主要用于城市交通管理部门,帮助执法人员快速识别和处理违反限号规定的车辆,提高交通管理的智能化水平。具体应用场景包括但不限于道路监控、停车场管理和临时检查点等。 其他说明:文中提供了大量Matlab代码示例,详细解释了各个步骤的具体实现方法。此外,作者还分享了许多实际开发过程中遇到的问题及其解决方案,有助于读者更好地理解和应用这些技术。

    2000-2017年各省国有经济煤气生产和供应业固定资产投资数据

    2000-2017年各省国有经济煤气生产和供应业固定资产投资数据 1、时间:2000-2017年 2、来源:国家统计j、能源nj 3、指标:行政区划代码、城市、年份、国有经济煤气生产和供应业固定资产投资 4、范围:31省

    HDRI大全三维环境切图

    HDRI大全三维环境切图

    ADAS系统核心技术解析:ACC、FCW、AEB、LKA的设计与实现

    内容概要:本文详细介绍了ADAS(高级驾驶辅助系统)中四个主要功能模块的设计与实现,分别是自适应巡航控制系统(ACC)、前向碰撞预警系统(FCW)、自动紧急制动系统(AEB)和车道保持辅助系统(LKA)。文章不仅展示了各个系统的具体算法实现,如ACC中的PID控制、FCW中的TTC计算、AEB中的状态机设计和LKA中的PD控制器,还分享了许多实际开发中的经验和挑战,如参数调校、传感器融合、时间同步等问题。此外,文中还提到了一些有趣的细节,如在暴雨天气下LKA的表现优化,以及AEB系统在测试过程中遇到的各种corner case。 适合人群:汽车电子工程师、自动驾驶研究人员、嵌入式软件开发者。 使用场景及目标:帮助读者深入了解ADAS系统的工作原理和技术细节,掌握关键算法的实现方法,提高在实际项目中的开发和调试能力。 其他说明:文章通过生动的语言和具体的代码示例,使复杂的理论变得通俗易懂,有助于初学者快速入门并深入理解ADAS系统的开发流程。

    (源码)基于PHP的历史年表聚合网站.zip

    # 基于PHP的历史年表聚合网站 ## 项目简介 本项目是一个历史年表的聚合网站,采用PHP编程语言开发。网站包含了众多功能函数,可处理系统信息、错误异常、数字、字符串、时间等,还具备数据库管理、搜索、用户管理等功能,同时拥有日志记录和安全防护模块。 ## 项目的主要特性和功能 1. 系统信息处理能获取系统名称、版本、描述等信息,还可管理令牌。 2. 错误处理具备生成错误消息、错误退出等功能。 3. 数据处理涵盖数字、字符串、时间的处理,如数字转汉字、字符串编码等。 4. 标签管理定义了多种与历史相关的标签信息,包括朝代、事件、人物等。 5. 数据库操作提供数据库管理、SQL查询语句生成、搜索、用户管理等功能。 6. 用户界面生成可生成和更新与时间、标签相关的用户界面元素。 7. 日志记录包含日志保存、格式化、核心记录等功能。 8. 安全防护拥有Web应用防火墙模块,增强网站安全性。 ## 安装使用步骤

Global site tag (gtag.js) - Google Analytics