如何彻底解决米游社自动签到难题:5步配置终极指南
2026/1/17 7:56:17
publicclassStudent{publicvoidstudy(StudentStudyObserverstudentStudyObserver){newThread(()->{try{TimeUnit.SECONDS.sleep(5);}catch(InterruptedExceptione){e.printStackTrace();}studentStudyObserver.onFinish();}).start();}}publicinterfaceStudentStudyObserver{voidonFinish();}CountDownLatchcountDownLatch=newCountDownLatch(1);Studentstudent=newStudent();student.study(()->{System.out.println("学生学习完了");countDownLatch.countDown();});try{countDownLatch.await();}catch(InterruptedExceptione){e.printStackTrace();}System.out.println("继续执行其他任务");CyclicBarriercyclicBarrier=newCyclicBarrier(2);Studentstudent=newStudent();student.study(()->{System.out.println("学生学习完了");try{cyclicBarrier.await();}catch(InterruptedException|BrokenBarrierExceptione){e.printStackTrace();}});try{cyclicBarrier.await();}catch(InterruptedException|BrokenBarrierExceptione){e.printStackTrace();}System.out.println("继续执行其他任务");privatevolatilebooleanfinished=false;Objectlock=newObject();Studentstudent=newStudent();student.study(()->{synchronized(lock){System.out.println("学生学习完了");finished=true;lock.notify();}});try{synchronized(lock){while(!finished){lock.wait();}}}catch(InterruptedExceptione){e.printStackTrace();}System.out.println("继续执行其他任务");