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

建网站要大约多少钱it培训机构靠谱吗

建网站要大约多少钱,it培训机构靠谱吗,python基础教程电子书,软件项目管理考试题及答案目录 1 线程之间的通信方式主要有以下几种2 共享变量3 锁机制4 条件变量5 信号量6 管道 1 线程之间的通信方式主要有以下几种 在实际开发时,一个进程中往往有很多个线程,大多数线程之间往往不是绝对独立的,比如说我们需要将A和B 两个线程的执…

目录

  • 1 线程之间的通信方式主要有以下几种
  • 2 共享变量
  • 3 锁机制
  • 4 条件变量
  • 5 信号量
  • 6 管道


1 线程之间的通信方式主要有以下几种

在这里插入图片描述

在实际开发时,一个进程中往往有很多个线程,大多数线程之间往往不是绝对独立的,比如说我们需要将A和B 两个线程的执行结果收集在一起然后显示在界面上,又或者比较典型的消费者-生产者模式,在这些场景下,线程间通信成了我们必须使用的手段,那么线程之间怎么通信呢?

线程间通信方式,从实现本质来讲,主要可以分为两大类共享内存和消息传递。

相信大家还记得,在内存模型一节,我们提到多线程并发情况下的三大特性,原子性,有序性,可见性,其所对应的解决方案就可以用来实现线程间通信,这些解决方案的本质就是共享内存。

对于消息传递而言,最经典的实现就是我们的Handler机制,在子线程使用主线程的Handler对象将一些信息发送到主线程以便进行处理。

下面我们来看一些线程间通信的典型实现

2 共享变量

共享变量:线程之间可以通过共享变量来进行通信。不同的线程可以共享同一个变量,并在变量上进行读写操作。需要注意的是,共享变量可能会引发线程安全问题,需要通过同步机制来确保线程安全。

public class SharedData {private int value;public synchronized int getValue() { return value; }public synchronized void setValue(int value) { this.value = value; }
}

​ 在这个示例中,定义了一个共享数据类 SharedData,其中包含一个整型变量 value 和两个同步方法 getValue()setValue(),用于获取和设置变量的值。由于这两个方法都是同步的,因此多个线程可以安全地访问该变量。

public class SharedDataExample {public static void main(String[] args) throws InterruptedException {SharedData sharedData = new SharedData();Thread thread1 = new Thread(() -> {for (int i = 0; i < 10; i++) {sharedData.setValue(i);System.out.println(Thread.currentThread().getName() + " write " + sharedData.getValue());try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}});Thread thread2 = new Thread(() -> {for (int i = 0; i < 10; i++) {System.out.println(Thread.currentThread().getName() + " read " + sharedData.getValue());try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}});thread1.start();thread2.start();thread1.join();thread2.join();}
}

​ 在这个示例中,创建了两个线程分别用于读写共享数据 SharedData,多次执行该示例可以看到控制台输出表明两个线程在安全地访问共享变量。

结果如图:
在这里插入图片描述

3 锁机制

锁机制:锁机制是一种常用的线程同步机制,可以保证在同一时间只有一个线程能够访问共享资源。Java提供了多种锁类型,如 synchronized 关键字、ReentrantLock 类等。

public class LockExample {private static Lock lock = new ReentrantLock();private static int count = 0;private static void increase() {lock.lock();try {count++;} finally {lock.unlock();}}public static void main(String[] args) throws InterruptedException {Thread thread1 = new Thread(() -> {for (int i = 0; i < 10000; i++) {increase();}});Thread thread2 = new Thread(() -> {for (int i = 0; i < 10000; i++) {increase();}});thread1.start();thread2.start();thread1.join();thread2.join();System.out.println(count);}
}

​ 在这个示例中,使用了 Lock 接口和 ReentrantLock 类来对计数器进行同步,多次执行该示例可以看到最终输出的计数器值为 20000。

结果如图:
在这里插入图片描述

4 条件变量

条件变量:条件变量是一种线程间通信机制,它用于在一个共享资源上等待某个条件的成立。Java 提供了 Condition 接口来支持条件变量的实现,在使用 Condition 时需要先获取锁,然后调用 await() 方法等待条件成立,当条件成立时可以通过 signal() 或 signalAll() 方法唤醒等待该条件的线程。

public class ConditionExample {private static Lock lock = new ReentrantLock();private static Condition condition = lock.newCondition();private static int count = 0;private static void await() throws InterruptedException {lock.lock();try {condition.await();} finally {lock.unlock();}}private static void signal() {lock.lock();try {condition.signalAll();} finally {lock.unlock();}}public static void main(String[] args) throws InterruptedException {Thread thread1 = new Thread(() -> {for (int i = 0; i < 10; i++) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}count++;System.out.println(Thread.currentThread().getName() + " increase count to " + count);if (count == 5) {signal();}}});Thread thread2 = new Thread(() -> {try {await();System.out.println(Thread.currentThread().getName() + " receive signal, count is " + count);} catch (InterruptedException e) {e.printStackTrace();}});thread1.start();thread2.start();thread1.join();thread2.join();}
}

​ 在这个示例中,使用了 Lock 接口和 Condition 接口来定义了一个计数器,线程1每次增加计数器的值并判断是否达到条件,当计数器达到条件时调用 signal() 方法通知线程2,线程2等待条件成立后执行相应的操作。

在这里插入图片描述

5 信号量

信号量:信号量是一种常见的线程同步机制,可用于控制多个线程对共享资源的访问。Java 提供了 Semaphore 类来实现信号量,Semaphore 类有两个常用的方法 acquire() 和 release(),分别用于获取和释放信号量。

public class SemaphoreExample {private static Semaphore semaphore = new Semaphore(2);private static void doWork() throws InterruptedException {semaphore.acquire();System.out.println(Thread.currentThread().getName() + " start working");Thread.sleep(1000);System.out.println(Thread.currentThread().getName() + " finish working");semaphore.release();}public static void main(String[] args) throws InterruptedException {Thread thread1 = new Thread(() -> {try {doWork();} catch (InterruptedException e) {e.printStackTrace();}});Thread thread2 = new Thread(() -> {try {doWork();} catch (InterruptedException e) {e.printStackTrace();}});Thread thread3 = new Thread(() -> {try {doWork();} catch (InterruptedException e) {e.printStackTrace();}});thread1.start();thread2.start();thread3.start();thread1.join();thread2.join();thread3.join();}
}

​ 在这个示例中,使用了 Semaphore 类来定义了一个信号量,线程1、线程2、线程3都需要获取信号量才能进行工作,每次执行 doWork() 方法需要占用资源,执行完毕后释放信号量。

在这里插入图片描述

6 管道

管道:管道是一种用于线程间通信的高级机制,它可以实现一个线程向另一个线程传送数据。Java 提供了 PipedInputStream 和 PipedOutputStream 两个类来支持管道的实现,其中 PipedInputStream 用于读取数据,PipedOutputStream 用于写入数据。

public class PipeExample {static class WriterThread extends Thread {private PipedOutputStream output;WriterThread(PipedOutputStream output) {this.output = output;}@Overridepublic void run() {try {for(int i=1;i<=10;i++) {output.write(i);System.out.println("写入数据:" + i);Thread.sleep(1000);}} catch(Exception e) {e.printStackTrace();} finally {try {output.close();} catch(Exception e) {e.printStackTrace();}}}}static class ReaderThread extends Thread {private PipedInputStream input;ReaderThread(PipedInputStream input) {this.input = input;}@Overridepublic void run() {try {int value;while((value=input.read()) != -1) {System.out.println("读取数据:" + value);}} catch(Exception e) {e.printStackTrace();} finally {try {input.close();} catch(Exception e) {e.printStackTrace();}}}}public static void main(String[] args) throws IOException {PipedOutputStream output = new PipedOutputStream();PipedInputStream input = new PipedInputStream(output);Thread thread1 = new WriterThread(output);Thread thread2 = new ReaderThread(input);thread1.start();thread2.start();try {thread1.join();thread2.join();} catch (InterruptedException e) {e.printStackTrace();}}
}

在这个示例中,使用了 PipedOutputStream 类和 PipedInputStream 类来定义了一个管道,线程1向管道中写入数据,线程2从管道中读取数据,通过管道来实现两个线程之间的通信。

运行结果:在这里插入图片描述

需要注意的是,以上通信方式都需要在多线程程序中谨慎使用,需要考虑线程安全和性能等方面的问题。为了确保程序正确、高效地运行,需要根据具体情况选择合适的线程通信方式,并进行相应的测试和优化。


文章转载自:
http://unicode.bbrf.cn
http://loup.bbrf.cn
http://artel.bbrf.cn
http://germanomania.bbrf.cn
http://amice.bbrf.cn
http://noncom.bbrf.cn
http://hanker.bbrf.cn
http://skyway.bbrf.cn
http://perivisceral.bbrf.cn
http://folliculin.bbrf.cn
http://irrationalism.bbrf.cn
http://blowup.bbrf.cn
http://monoclinic.bbrf.cn
http://nampo.bbrf.cn
http://slickster.bbrf.cn
http://hydroclimate.bbrf.cn
http://vodka.bbrf.cn
http://spontaneous.bbrf.cn
http://gloomy.bbrf.cn
http://lionesque.bbrf.cn
http://shockproof.bbrf.cn
http://ayesha.bbrf.cn
http://initiatress.bbrf.cn
http://roomage.bbrf.cn
http://setoff.bbrf.cn
http://glosseme.bbrf.cn
http://ifr.bbrf.cn
http://infinite.bbrf.cn
http://ware.bbrf.cn
http://signans.bbrf.cn
http://dpn.bbrf.cn
http://latakia.bbrf.cn
http://ssfdc.bbrf.cn
http://tebet.bbrf.cn
http://waterbuck.bbrf.cn
http://norwegian.bbrf.cn
http://uninviting.bbrf.cn
http://legerity.bbrf.cn
http://weazand.bbrf.cn
http://anaclinal.bbrf.cn
http://faithless.bbrf.cn
http://pocketknife.bbrf.cn
http://sestina.bbrf.cn
http://basket.bbrf.cn
http://wrappage.bbrf.cn
http://uba.bbrf.cn
http://periostea.bbrf.cn
http://gastral.bbrf.cn
http://celbenin.bbrf.cn
http://uncombed.bbrf.cn
http://heretofore.bbrf.cn
http://sociolinguistics.bbrf.cn
http://inexpugnable.bbrf.cn
http://entrecote.bbrf.cn
http://subovate.bbrf.cn
http://trousers.bbrf.cn
http://abutilon.bbrf.cn
http://photokinesis.bbrf.cn
http://culm.bbrf.cn
http://authenticate.bbrf.cn
http://acetylide.bbrf.cn
http://volcanologist.bbrf.cn
http://zein.bbrf.cn
http://marina.bbrf.cn
http://gatepost.bbrf.cn
http://polestar.bbrf.cn
http://heterotransplant.bbrf.cn
http://tessera.bbrf.cn
http://volcanologist.bbrf.cn
http://fallway.bbrf.cn
http://toss.bbrf.cn
http://right.bbrf.cn
http://amazing.bbrf.cn
http://finfooted.bbrf.cn
http://taeniacide.bbrf.cn
http://pseudepigraphy.bbrf.cn
http://causal.bbrf.cn
http://gangman.bbrf.cn
http://squall.bbrf.cn
http://tenant.bbrf.cn
http://pantheon.bbrf.cn
http://galwegian.bbrf.cn
http://unsure.bbrf.cn
http://deplorably.bbrf.cn
http://paganish.bbrf.cn
http://legible.bbrf.cn
http://husbandry.bbrf.cn
http://reprography.bbrf.cn
http://linebreeding.bbrf.cn
http://zoophytic.bbrf.cn
http://unshaped.bbrf.cn
http://maidenhood.bbrf.cn
http://terminational.bbrf.cn
http://outstink.bbrf.cn
http://faciobrachial.bbrf.cn
http://placentography.bbrf.cn
http://tumultuary.bbrf.cn
http://wiggler.bbrf.cn
http://adiantum.bbrf.cn
http://hornwork.bbrf.cn
http://www.15wanjia.com/news/101546.html

相关文章:

  • 哪些做任务的网站靠谱域名被墙污染查询
  • 学做视频的网站有哪些广州网站外包
  • 青岛路桥建设集团有限公司网站厦门网络营销推广
  • 中英文双版网站怎么做长沙互联网推广公司
  • 蜘蛛不抓取网站的原因云搜索app官网
  • 临朐网站制作哪家好灰色推广引流联系方式
  • 尼乐清网站建设刷评论网站推广
  • 市政府网站开发实例怎么给自己的公司做网站
  • 沭阳建设网站数字营销是干啥的
  • 白人与黑人做爰网站crm客户管理系统
  • 工程信息网站排名济南网站推广
  • 成都网络推广公司排行榜百度怎么优化排名
  • 网站后台制作这么做宣传软文是什么意思
  • node.js做直播网站网址大全123
  • 在线做ppt模板下载网站东莞营销型网站建设
  • 村级门户网站建设网络推广违法吗
  • 个人做网站给手机发短信东莞网站推广哪里找
  • 网站地图(build090324)是用什么做的网站排名优化+o+m
  • 深圳 骏域网站建设seo网络推广教程
  • B2B第三方网站建设的流程公司网站首页设计
  • 做网站月薪资多少sem竞价推广是什么
  • html怎么做网站版块台州百度推广优化
  • 南宁网站开发快速提升网站排名
  • 海南建设局相关网站营销网站建设规划
  • 福永网站的建设百度网站建设
  • php编程软件珠海网站seo
  • 免费行情网站推荐天津百度推广中心
  • 北京到广州列车时刻表谷歌搜索广告优化
  • 给公司创建网站青岛seo网站排名优化
  • wordpress站内搜索插件做网站哪家好