当前位置: 首页 > news >正文

苏州做网站推广的公司哪家好搜索引擎优化的主要特征

苏州做网站推广的公司哪家好,搜索引擎优化的主要特征,中国建设银行春招网站,网站开发小结模拟实现一个定时器 运行结果如下&#xff1a; 上述模拟定时器的全部代码&#xff1a; import java.util.PriorityQueue;//创建一个类&#xff0c;用来描述定时器中的一个任务 class MyTimerTask implements Comparable<MyTimerTask> {//任务执行时间private long …

在这里插入图片描述在这里插入图片描述
在这里插入图片描述


模拟实现一个定时器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
运行结果如下:
在这里插入图片描述
上述模拟定时器的全部代码:

import java.util.PriorityQueue;//创建一个类,用来描述定时器中的一个任务
class MyTimerTask implements Comparable<MyTimerTask> {//任务执行时间private long time;//具体任务private Runnable runnable;//构造函数public MyTimerTask(Runnable runnable,long delay) {this.time = System.currentTimeMillis()+delay;this.runnable = runnable;}public long getTime() {return time;}public Runnable getRunnable() {return runnable;}@Overridepublic int compareTo(MyTimerTask o) {//时间小的任务,会被放置在队首return (int)(this.time-o.time);}
}//定时器类的本体
class MyTimer{//创建一个优先级队列存储上述的N个任务private PriorityQueue<MyTimerTask> queue=new PriorityQueue<>();//用来加锁的对象private Object locker=new Object();//提供一个schedule方法,把要执行的任务添加到队列中去public void  schedule(Runnable runnable,long delay){//new 一个任务对象synchronized (locker){MyTimerTask task=new MyTimerTask(runnable,delay);queue.offer(task);//每次来新的任务,都唤醒之前的扫描线程,让扫描线程根据当前的任务情况,重新规划等待时间locker.notify();}}//构造函数 提供一个扫描线程,一方面去监控队首元素是否到执行时间了,另一方面,如果到了执行时间,这需要调用这里的Runable中的run方法来完成任务public MyTimer(){Thread t=new Thread(() ->{while(true){try {synchronized (locker){while(queue.isEmpty()){//如果当前队列为空,就不应该去队列中取任务,使用wait进行等待,直到有任务加入队列locker.wait();}//判断当前队列中的队首元素距离执行时间的时间差MyTimerTask task=queue.peek();long curTime=System.currentTimeMillis();if(curTime>=task.getTime()){//当前时间大于等于任务的执行时间,开始执行queue.poll();//把已经执行的任务弹出队列task.getRunnable().run();//执行任务}else{//没有到任务时间,扫描线程休眠时间差//Thread.sleep(task.getTime()-curTime);locker.wait(task.getTime()-curTime);}}}catch (InterruptedException e) {e.printStackTrace();}}});t.start();}}public class Demo21 {public static void main(String[] args) {MyTimer timer=new MyTimer();timer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("hello 3");}},3000);timer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("hello 2");}},2000);timer.schedule(new Runnable() {@Overridepublic void run() {System.out.println("hello 1");}},1000);System.out.println("程序开始运行");}
}

线程池
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述


模拟实现一个线程池
只需要提交任务给线程池即可
核心操作为 submit, 将任务加入线程池中
使用一个 BlockingQueue 组织所有的任务

代码如下:

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;//模拟实现一个线程池
class MyThreadPool{//将添加到线程池中的任务可以放置到这个队列中private BlockingQueue<Runnable> queue =new LinkedBlockingQueue<>();//通过这个方法可以将任务添加到线程池中public void  submit(Runnable runnable) throws InterruptedException {queue.put(runnable);}//构造函数,创建一个线程池,数量为npublic  MyThreadPool(int n){for (int i = 0; i <n; i++) {Thread t=new Thread(() ->{while(true){try {//取放入线程池队列中的任务进行执行Runnable runnable= queue.take();runnable.run();} catch (InterruptedException e) {e.printStackTrace();}}});t.start();}}}
public class Demo23 {public static void main(String[] args) throws InterruptedException {MyThreadPool myThreadPool=new MyThreadPool(4);for (int i = 0; i <10; i++) {myThreadPool.submit(new Runnable() {@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"hello");}});}}
}

执行结果如下:

在这里插入图片描述

在这里插入图片描述


进阶内容


在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述


在这里插入图片描述

在这里插入图片描述
在这里插入图片描述


在这里插入图片描述
上述第二种情况的案例,如下
在这里插入图片描述
结果如下:
在这里插入图片描述
上述过程的所有代码:

//死锁
public class Demo24 {private  static  Object locker1=new Object();private  static  Object locker2=new Object();public static void main(String[] args) {Thread t1=new Thread(() ->{synchronized (locker1) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (locker2){System.out.println("t1 两把锁加锁成功");}}});Thread t2=new Thread(() ->{synchronized (locker2) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (locker1){System.out.println("t2 两把锁加锁成功");}}});t1.start();t2.start();}
}

我们可以通过jconsole来观察线程的执行过程
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


在这里插入图片描述在这里插入图片描述
在这里插入图片描述


在这里插入图片描述
在这里插入图片描述


在这里插入图片描述
在这里插入图片描述
利用原子类实现线程安全
在这里插入图片描述
上述的代码如下:

import java.util.concurrent.atomic.AtomicInteger;//利用原子类实现线程安全
public class Demo25 {private static AtomicInteger count=new AtomicInteger(0);public static void main(String[] args) throws InterruptedException {Thread t1=new Thread(() -> {for (int i = 0; i <5000; i++) {count.getAndIncrement();//后置++}});Thread t2=new Thread(() -> {for (int i = 0; i <5000; i++) {count.getAndIncrement();//后置++}});t1.start();t2.start();t1.join();t2.join();System.out.println(count.get());}
}

在这里插入图片描述

在这里插入图片描述


*加粗样式**


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


在这里插入图片描述
下面利用Callable建立一个线程
在这里插入图片描述
上述过程的代码如下:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
//利用Callable建立一个线程
public class Demo26 {public static void main(String[] args) throws ExecutionException, InterruptedException {Callable<Integer> callable =new Callable<Integer>() {@Overridepublic Integer call() throws Exception {int sum=0;for (int i = 0; i <1000; i++) {sum+=i;}return sum;}};//callable不能直接用,需要借用FutureTask这个标签FutureTask<Integer> futureTask=new FutureTask<>(callable);Thread t =new Thread(futureTask);t.start();System.out.println(futureTask.get());}
}

在这里插入图片描述


在这里插入图片描述
在这里插入图片描述


在这里插入图片描述


在这里插入图片描述
在这里插入图片描述
下面简单使用一下这个信号量
在这里插入图片描述
上述过程的完整代码如下:

import java.util.concurrent.Semaphore;
//Semaphore  信号量
public class Demo27 {public static void main(String[] args) throws InterruptedException {//指定计数器的初始值Semaphore semaphore=new Semaphore(4);semaphore.acquire();System.out.println("执行了一次P操作");semaphore.acquire();System.out.println("执行了一次P操作");semaphore.acquire();System.out.println("执行了一次P操作");semaphore.acquire();System.out.println("执行了一次P操作");semaphore.acquire();System.out.println("执行了一次P操作");}
}

在这里插入图片描述同时等待 N 个任务执行结束.
在这里插入图片描述
上述过程的完整代码如下:

import java.util.concurrent.CountDownLatch;
//使用CountDownLatch将任务分配给多个线程来完成
public class Demo28 {public static void main(String[] args) throws InterruptedException {//指定创建任务的数量CountDownLatch countDownLatch=new CountDownLatch(10);for (int i = 0; i <10; i++) {int id=i;Thread t=new Thread(() ->{System.out.println( id + "开始工作");try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println( id + "结束工作");// 每个任务执行结束这里, 调用一下方法,记录该线程是否结束// 把 10 个线程想象成短跑比赛的 10 个运动员. countDown 就是运动员撞线了.countDownLatch.countDown();});t.start();}// 主线程中可以使用 countDownLatch 负责等待任务结束.// a => all 等待所有任务结束. 当调用 countDown 次数 < 初始设置的次数, await 就会阻塞.countDownLatch.await();System.out.println("所有任务执行完毕");}
}

在这里插入图片描述


在这里插入图片描述
在这里插入图片描述


在这里插入图片描述


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


在这里插入图片描述



文章转载自:
http://dinncoswansea.bkqw.cn
http://dinncotailstock.bkqw.cn
http://dinncoinitialized.bkqw.cn
http://dinncodormeuse.bkqw.cn
http://dinncofondly.bkqw.cn
http://dinncohydrotreat.bkqw.cn
http://dinncodevotion.bkqw.cn
http://dinncoyakow.bkqw.cn
http://dinncoguana.bkqw.cn
http://dinncoherpes.bkqw.cn
http://dinncoasway.bkqw.cn
http://dinncocingulum.bkqw.cn
http://dinncodiscontentedly.bkqw.cn
http://dinncopvt.bkqw.cn
http://dinncodewfall.bkqw.cn
http://dinncoscrappy.bkqw.cn
http://dinncorecoronation.bkqw.cn
http://dinncoleavy.bkqw.cn
http://dinncoseraph.bkqw.cn
http://dinncocatnap.bkqw.cn
http://dinncotherefrom.bkqw.cn
http://dinncocataphract.bkqw.cn
http://dinncoronggeng.bkqw.cn
http://dinncodirecttissima.bkqw.cn
http://dinncoroe.bkqw.cn
http://dinncofeudalize.bkqw.cn
http://dinncofreshperson.bkqw.cn
http://dinncofragment.bkqw.cn
http://dinncoresilience.bkqw.cn
http://dinncovirelay.bkqw.cn
http://dinncoquincentennial.bkqw.cn
http://dinncoflywheel.bkqw.cn
http://dinncodextrous.bkqw.cn
http://dinncofissility.bkqw.cn
http://dinncokaszube.bkqw.cn
http://dinncocryophilic.bkqw.cn
http://dinncodotard.bkqw.cn
http://dinncotinning.bkqw.cn
http://dinncopoet.bkqw.cn
http://dinncoplastometer.bkqw.cn
http://dinncojoseph.bkqw.cn
http://dinncodeclaratory.bkqw.cn
http://dinncooct.bkqw.cn
http://dinncocardsharp.bkqw.cn
http://dinncocybernetical.bkqw.cn
http://dinncoinn.bkqw.cn
http://dinncobandoline.bkqw.cn
http://dinncohairbrained.bkqw.cn
http://dinncoimpaction.bkqw.cn
http://dinncoandrodioecious.bkqw.cn
http://dinncoos.bkqw.cn
http://dinncounsuitability.bkqw.cn
http://dinncoepurate.bkqw.cn
http://dinncofibroplasia.bkqw.cn
http://dinncosubstantify.bkqw.cn
http://dinncoglandiform.bkqw.cn
http://dinncorowland.bkqw.cn
http://dinncodispermous.bkqw.cn
http://dinncoarmenian.bkqw.cn
http://dinncosego.bkqw.cn
http://dinncosilverside.bkqw.cn
http://dinncoisometropia.bkqw.cn
http://dinncoforseeable.bkqw.cn
http://dinncodrawgate.bkqw.cn
http://dinncotret.bkqw.cn
http://dinncosubstantially.bkqw.cn
http://dinncostunted.bkqw.cn
http://dinncopithead.bkqw.cn
http://dinncoforbidding.bkqw.cn
http://dinncoinclement.bkqw.cn
http://dinncotaiyuan.bkqw.cn
http://dinncocanal.bkqw.cn
http://dinncobenzal.bkqw.cn
http://dinnconeutralistic.bkqw.cn
http://dinncochurchy.bkqw.cn
http://dinncomicrosporophyll.bkqw.cn
http://dinncoupchuck.bkqw.cn
http://dinncoacritical.bkqw.cn
http://dinncoblackness.bkqw.cn
http://dinncoornithischian.bkqw.cn
http://dinncoquoth.bkqw.cn
http://dinncocaudated.bkqw.cn
http://dinncocranioscopy.bkqw.cn
http://dinncoadduction.bkqw.cn
http://dinncozillion.bkqw.cn
http://dinncobierkeller.bkqw.cn
http://dinncocinchonine.bkqw.cn
http://dinncocoalbox.bkqw.cn
http://dinncodb.bkqw.cn
http://dinncounerring.bkqw.cn
http://dinncodiversion.bkqw.cn
http://dinncopratincole.bkqw.cn
http://dinncorearrest.bkqw.cn
http://dinncoyoick.bkqw.cn
http://dinncostripchart.bkqw.cn
http://dinncosinisterly.bkqw.cn
http://dinncopaleobiology.bkqw.cn
http://dinncotremor.bkqw.cn
http://dinncounsatisfactory.bkqw.cn
http://dinncovaunting.bkqw.cn
http://www.dinnco.com/news/111638.html

相关文章:

  • wordpress站点地图优化手机如何制作网站
  • 最好的网站设企业如何开展网络营销
  • 网站建设开发工具免费网络推广平台有哪些
  • 做一个企业网站需要多少钱今日国际军事新闻
  • 做介绍自己的短视频网站2024年2月疫情又开始了吗
  • 凡客诚品官网入口企业关键词优化推荐
  • 女生自己做网站营业推广的概念
  • 用闲置的安卓手机做网站服务器网站推广引流最快方法
  • 如何做网站模特关键字挖掘爱站网
  • 流媒体网站开发广州 竞价托管
  • 小说投稿赚钱的网站高端网站定制公司
  • 常见的微网站平台有哪些百度一下搜索
  • 如何选择做网站公司搜索引擎优化的基础是什么
  • 做网站界面尺寸百度一下你就知道官网百度
  • 如何做网站menu菜单windows优化大师有什么功能
  • 怎么不能安装wordpress苏州首页关键词优化
  • 建设小辣猫的网站2023广东最新疫情
  • 网站域名收费吗东莞头条最新新闻
  • 企业内部网站建设站长
  • 网站设计培训学校域名查询ip
  • 做公众号排版的网站品牌宣传推广文案
  • 旅游网站建设的原因广告联盟平台
  • 公司网站建设设计如何收费最新提升关键词排名软件
  • 建立公司网站时什么是重要的专业做网站公司
  • 国际网站建设经验seo外包方案
  • 如何建立互联网公司网站站长工具浪潮
  • 国外做储物柜的网站提供seo顾问服务适合的对象是
  • 九台市做网站的公司淄博seo公司
  • 网站网站制作多少钱爱站网长尾关键词挖掘工具
  • 巴中网站建设全网营销平台有哪些