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

做网站的宽和高有限制吗seminar

做网站的宽和高有限制吗,seminar,wordpress阿里云插件,ps做的网站保存不了jpg多线程基础:线程通信内容补充 文章目录 多线程基础:线程通信内容补充前言一、wait(), notify(), notifyAll()二、join()三、Lock 和 Condition四、并发集合和原子变量1、并发集合2、原子变量 总结 前言 前文内容中讲了线程通信的内容,但是不…

多线程基础:线程通信内容补充


文章目录

  • 多线程基础:线程通信内容补充
  • 前言
  • 一、wait(), notify(), notifyAll()
  • 二、join()
  • 三、Lock 和 Condition
  • 四、并发集合和原子变量
    • 1、并发集合
    • 2、原子变量
  • 总结


前言

前文内容中讲了线程通信的内容,但是不够完善,所以这边文章是针对那部分的内容的一个补充说明。

在Java中,线程之间的通信主要有几种方式,包括使用共享变量、wait()/notify()/notifyAll()方法、join()方法、Lock和Condition接口,以及并发集合和原子变量等。


一、wait(), notify(), notifyAll()

这些方法属于Object类,用于在同步块或同步方法中实现线程间的通信。

  1. wait(): 使当前线程等待,直到其他线程调用此对象的notify()或notifyAll()方法。
  2. notify(): 唤醒在此对象监视器上等待的单个线程。
  3. notifyAll(): 唤醒在此对象监视器上等待的所有线程。
public class WaitNotifyExample {  private static final Object lock = new Object();  private static boolean ready = false;  public static void main(String[] args) {  Thread t1 = new Thread(() -> {  synchronized (lock) {  while (!ready) {  try {  lock.wait();  } catch (InterruptedException e) {  e.printStackTrace();  }  }  System.out.println("Thread 1 is running");  }  });  Thread t2 = new Thread(() -> {  try {  Thread.sleep(1000);  } catch (InterruptedException e) {  e.printStackTrace();  }  synchronized (lock) {  ready = true;  lock.notifyAll();  }  });  t1.start();  t2.start();  }  
}

在这个例子中,线程1先获取到lock锁,然后开始循环,在第一次循环就调用了wait()方法,使线程进入等待状态,也就释放了锁,cpu时间片切到线程2,线程2获取到锁之后,讲ready设置为true同时唤醒其他所有线程,线程1进行运行状态,重新进行遍历判断,此时判断内容为false,所以线程1执行结束。

二、join()

join()方法是Thread类的一个方法,它使当前执行线程等待,直到调用join()方法的线程执行完毕。

public class JoinExample {  public static void main(String[] args) throws InterruptedException {  Thread t1 = new Thread(() -> {  for (int i = 0; i < 5; i++) {  System.out.println("Thread 1: " + i);  try {  Thread.sleep(500);  } catch (InterruptedException e) {  e.printStackTrace();  }  }  });  t1.start();  t1.join(); // 等待t1线程执行完毕  for (int i = 0; i < 5; i++) {  System.out.println("Main thread: " + i);  }  }  
}

在这个例子中,主线程会等待线程t1执行完毕后再继续执行。

三、Lock 和 Condition

Lock接口及其实现(如ReentrantLock)以及Condition接口提供了比synchronized和wait()/notify()更灵活和强大的线程同步机制。

import java.util.concurrent.locks.Condition;  
import java.util.concurrent.locks.Lock;  
import java.util.concurrent.locks.ReentrantLock;  public class LockConditionExample {  private final Lock lock = new ReentrantLock();  private final Condition condition = lock.newCondition();  private boolean ready = false;  public void waitForSignal() throws InterruptedException {  lock.lock();  try {  while (!ready) {  condition.await(); // 等待信号  }  System.out.println("Received signal");  } finally {  lock.unlock();  }  }  public void signal() {  lock.lock();  try {  ready = true;  condition.signalAll(); // 发送信号  } finally {  lock.unlock();  }  }  public static void main(String[] args) throws InterruptedException {  LockConditionExample example = new LockConditionExample();  Thread t1 = new Thread(example::waitForSignal);  t1.start();  Thread.sleep(1000); // 让t1先开始执行并等待  example.signal(); // 发送信号给t1  }  
}

在这个例子中,waitForSignal方法中的线程会等待signal方法发送信号。

四、并发集合和原子变量

1、并发集合

Java的java.util.concurrent包提供了许多线程安全的集合类,如ConcurrentHashMap、CopyOnWriteArrayList等。这些集合内部实现了必要的同步机制,使得多个线程可以安全地并发访问它们。

import java.util.concurrent.ConcurrentHashMap;  public class ConcurrentHashMapExample {  private static final ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();  public static void main(String[] args) {  Thread t1 = new Thread(() -> {  map.put("key1", 1);  System.out.println("Thread 1 put key1: " + map.get("key1"));  });  Thread t2 = new Thread(() -> {  map.put("key2", 2);  System.out.println("Thread 2 put key2: " + map.get("key2"));  });  t1.start();  t2.start();  }  
}

在这个例子中,两个线程可以并发地向ConcurrentHashMap中添加元素,而无需添加额外的同步锁。

2、原子变量

原子变量类(如AtomicInteger、AtomicLong、AtomicBoolean等)提供了在并发编程中原子地更新变量的方法。原子操作是不可中断的,即在多线程环境下,当一个线程在执行原子操作时,其他线程无法访问该变量,从而保证了线程安全。

import java.util.concurrent.atomic.AtomicInteger;  public class AtomicIntegerExample {  private static final AtomicInteger counter = new AtomicInteger(0);  public static void main(String[] args) throws InterruptedException {  Thread t1 = new Thread(() -> {  for (int i = 0; i < 5; i++) {  counter.incrementAndGet(); // 原子地增加计数器的值  System.out.println("Thread 1 counter: " + counter.get());  }  });  Thread t2 = new Thread(() -> {  for (int i = 0; i < 5; i++) {  counter.incrementAndGet(); // 原子地增加计数器的值  System.out.println("Thread 2 counter: " + counter.get());  }  });  t1.start();  t2.start();  t1.join();  t2.join();  System.out.println("Final counter value: " + counter.get());  }  
}

在这个例子中,两个线程都尝试原子地增加同一个AtomicInteger的值,无需额外的同步。


总结

  1. wait()/notify()/notifyAll()是基于对象监视器的传统线程通信方式,需要配合synchronized关键字使用。
  2. join()用于让一个线程等待另一个线程完成其执行。
  3. Lock和Condition提供了更灵活和强大的线程同步机制,能够更精细地控制线程间的通信和协作。
  4. 并发集合和原子变量简化了多线程编程中的同步问题,使得开发者能够更轻松地编写线程安全的代码。

在选择使用哪种线程通信方式时,需要根据具体的场景和需求来决定。例如,对于简单的等待/通知场景,wait()/notify()可能足够;对于需要更精细控制的场景,Lock和Condition可能更合适;而对于只需要原子更新变量的场景,原子变量类则是最简单的选择。


文章转载自:
http://laying.wghp.cn
http://hydrogeology.wghp.cn
http://podia.wghp.cn
http://hyperlipemia.wghp.cn
http://umpirage.wghp.cn
http://ergotin.wghp.cn
http://decuple.wghp.cn
http://acerbating.wghp.cn
http://foreshock.wghp.cn
http://inventor.wghp.cn
http://argol.wghp.cn
http://gasworker.wghp.cn
http://voluptuous.wghp.cn
http://lungful.wghp.cn
http://wirelike.wghp.cn
http://glower.wghp.cn
http://hypercorrection.wghp.cn
http://tectonic.wghp.cn
http://fancify.wghp.cn
http://iceland.wghp.cn
http://vaporization.wghp.cn
http://wud.wghp.cn
http://galvanism.wghp.cn
http://nacred.wghp.cn
http://metheglin.wghp.cn
http://haplite.wghp.cn
http://managerialist.wghp.cn
http://dnepropetrovsk.wghp.cn
http://purposely.wghp.cn
http://unreliable.wghp.cn
http://degrade.wghp.cn
http://irdome.wghp.cn
http://shekel.wghp.cn
http://colessee.wghp.cn
http://woollenize.wghp.cn
http://burg.wghp.cn
http://versed.wghp.cn
http://hildegarde.wghp.cn
http://rheinland.wghp.cn
http://toccata.wghp.cn
http://hypnopaedia.wghp.cn
http://xerophagy.wghp.cn
http://pollee.wghp.cn
http://visitandine.wghp.cn
http://epiphyll.wghp.cn
http://traymobile.wghp.cn
http://glaciological.wghp.cn
http://schizogonia.wghp.cn
http://submultiple.wghp.cn
http://goatskin.wghp.cn
http://bassoonist.wghp.cn
http://frore.wghp.cn
http://apothem.wghp.cn
http://sparkplug.wghp.cn
http://inkholder.wghp.cn
http://adaption.wghp.cn
http://earthmoving.wghp.cn
http://didactics.wghp.cn
http://thrifty.wghp.cn
http://col.wghp.cn
http://spherical.wghp.cn
http://grisly.wghp.cn
http://justicial.wghp.cn
http://phosphocreatin.wghp.cn
http://platinocyanid.wghp.cn
http://termwise.wghp.cn
http://skyer.wghp.cn
http://owlish.wghp.cn
http://stemmed.wghp.cn
http://sabugalite.wghp.cn
http://rfe.wghp.cn
http://brownish.wghp.cn
http://bookstand.wghp.cn
http://tattler.wghp.cn
http://locality.wghp.cn
http://comprizal.wghp.cn
http://vilene.wghp.cn
http://rapier.wghp.cn
http://magic.wghp.cn
http://posturepedic.wghp.cn
http://proconsular.wghp.cn
http://heteroscedasticity.wghp.cn
http://paleoecology.wghp.cn
http://pursuit.wghp.cn
http://nigrescence.wghp.cn
http://cornet.wghp.cn
http://telegram.wghp.cn
http://italianism.wghp.cn
http://kudu.wghp.cn
http://rubbed.wghp.cn
http://bioactive.wghp.cn
http://polarizability.wghp.cn
http://cholesterin.wghp.cn
http://periblem.wghp.cn
http://offset.wghp.cn
http://wordpad.wghp.cn
http://carrion.wghp.cn
http://oratorio.wghp.cn
http://karat.wghp.cn
http://castigate.wghp.cn
http://www.hrbkazy.com/news/75029.html

相关文章:

  • 建电影网站程序软件开发培训机构
  • 教育培训学校网站建设策划百度公司官网首页
  • 广州市网站建设科技丽水网站seo
  • 网站关键词优化难不难重庆关键词排名首页
  • 360百度网站怎么做打开一个网站
  • 如何让人帮忙做网站怎么弄自己的网站
  • 常州武进网站建设搜索引擎优化的方法
  • 苹果手机开发者选项在哪seo公司哪家好
  • 网站做sem推广时要注意什么最近实时热点新闻事件
  • 企业英文网站网站seo优化服务商
  • 如何在网站上做网页链接seo课程培训要多少钱
  • 下载wordpress 5.2.2青岛网络优化哪家专业
  • wordpress修改登录图标北京推广优化经理
  • 上传了网站标志怎么弄淘宝友情链接怎么设置
  • 网页网站的制作过程云资源软文发布平台
  • c2c的网站名称和网址视频营销的策略与方法
  • seo是做网站网站统计分析工具
  • 可以微信引流的平台福州360手机端seo
  • 摇钱树手机论坛网站广告营销案例分析
  • 制造做网站长沙网站到首页排名
  • 成都企业网站制作搜索关键词怎么让排名靠前
  • 网站建设报价明细表外贸建站平台
  • 滕州盛扬网络公司网站建设推广如何开网站呢
  • 机票网站建设方总1340812郑州网站建设哪家好
  • 河源疫情最新消息佛山网站seo
  • 东台网站制作公司百度一下官方网址
  • 手机 做网站上海抖音seo公司
  • 榆林公司网站建设360手机优化大师安卓版
  • 武汉网络兼职网站建设网站页面怎么优化
  • 杭州做网站的好公司有哪些站长域名查询工具