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

电商网站的建设动态新浪云wordpress教程

电商网站的建设动态,新浪云wordpress教程,wordpress页面音乐,我的世界是怎么做的视频网站并发和并行 并发: 在同一时刻,多个指令在单一CPU上交替指向 并行:在同一时刻,多个指令在多个CPU上同时执行 2核4线程,4核8线程,8核16线程,16核32线程 基础实现线程的方式 Thread :继承类 &…

并发和并行

并发: 在同一时刻,多个指令在单一CPU上交替指向

并行:在同一时刻,多个指令在多个CPU上同时执行

2核4线程,4核8线程,8核16线程,16核32线程

基础实现线程的方式

  • Thread :继承类 ,无返回
  • Runnable :实现接口,无返回
  • Callable 核 TaskFurture : 实现接口,有返回
public static void main(String[] args) {MyThread t1 = new MyThread();t1.setName("自定义线程-1");MyThread t2 = new MyThread("自定义线程-2");t1.start();t2.start();}static class MyThread extends  Thread{//        int ticket = 100; // 单个线程私有
//        static int ticket = 100; // MyThread类启动的多线程共有public MyThread() {}public MyThread(String name) {super(name);}@Overridepublic void run() {for (int i=1;i<1000;i++){System.out.println(Thread.currentThread().getName()+"   "+i);}}}
public class RunnableMain {public static void main(String[] args) {MyRunnable r1 = new MyRunnable();Thread t1 = new Thread(r1,"自定义线程-1");Thread t2 = new Thread(t1,"自定义线程-2");t1.start();t2.start();}static class MyRunnable implements Runnable{// int ticket = 100; // 线程共享@Overridepublic void run() {for (int i=1;i<1000;i++){System.out.println(Thread.currentThread().getName()+"   "+i);}}}
}
public class CallableMain {public static void main(String[] args) throws ExecutionException, InterruptedException {MyCallable c1 = new MyCallable();FutureTask<Integer> f1 = new FutureTask<>(c1);Thread t1 = new Thread(f1);t1.start();System.out.println(f1.get());}static class MyCallable implements Callable<Integer>{@Overridepublic Integer call() throws Exception {Integer count = 0;for (int i=1;i<1000;i++){count+=i;}return count;}}
}

常见的成员方法

方法名称说明
String getName()返回此线程的名称
void setName()设置线程的名称
static Thread currentThread()获得当前线程对象
static void sleep(long time)让当前线程休眠,单位:毫秒
setPriority(int newPriority)设置线程优先级,1-10,10最大优先级,默认5
final int getPriority()获取线程的优先级
final void setDaemon(boolean on)设置为守护线程,其他非守护线程的线程结果,守护线程也将结束
public static void yield()礼让线程,让线程跑得更均匀
public staic void join()插入线程,插入得线程跑完再继续执行被插入线程

生命周期

同步代码块synchronized (),同步方法,local锁

// lock是接口,用ReentrantLock实现
Lock reentrantLock = new ReentrantLock();
reentrantLock.lock();
reentrantLock.unlock();
注意退出的时候,是否带锁退出,最好放在finally块里

死锁:相互等待

等待/唤醒

锁.wait();
锁.notifyAll(); //唤醒所有这两个方法必须在同步代码块内部调用

交替执行: 

public class MyArrayBlockingQueue {static Integer count = 10;static Integer hasFood = 0;static Object obj = new Object();public static void main(String[] args) {Cooker cooker = new Cooker();Eater eater = new Eater();Thread t1 = new Thread(cooker);Thread t2 = new Thread(eater);t1.start();t2.start();}@Datastaticclass Cooker implements Runnable {private ArrayBlockingQueue abq;@Overridepublic void run() {while (true) {if (count == 0) {break;} else {synchronized (MyArrayBlockingQueue.obj) {if (hasFood == 1) {// 有食物try {MyArrayBlockingQueue.obj.wait();} catch (InterruptedException e) {e.printStackTrace();}} else {hasFood++;System.out.println("做了一份菜");MyArrayBlockingQueue.obj.notifyAll();}}}}}}@Datastaticclass Eater implements Runnable {private ArrayBlockingQueue abq;@Overridepublic void run() {while (true) {if (count == 0) {break;} else {synchronized (MyArrayBlockingQueue.obj) {if (hasFood == 0) {// 没有食物try {MyArrayBlockingQueue.obj.wait();} catch (InterruptedException e) {e.printStackTrace();}} else {hasFood--;count--;System.out.println("吃了一份菜,还剩下" + count + "容量的肚子");MyArrayBlockingQueue.obj.notifyAll();}}}}}}}

阻塞队列

继承:

Iterable -> Collection -> Queue -> BlockingQueue -> ArrayBlockQueue(数组有界) / LinkedBlockQueue(数组无界)

线程池

// 获取一个单线程的线程池:
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();// 获取FixThread 线程池: 指定活跃线程数量2
ExecutorService executorService = Executors.newFixedThreadPool(2);// 创建一个可缓存的线程连接池,无限大(完全取决于操作系统最大允许多少)
// 超过60秒自动回收
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();// 1. 获取周期性线程池, 传入核心线程的大小
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

多线程(二) | 彻底搞懂线程池-Executors_executor.submit-CSDN博客

http://www.yayakq.cn/news/315656/

相关文章:

  • 百度包年推广网站建设广告设计培训目的
  • iis网站服务器基本安全设置步骤龙采做网站要多少钱
  • 吐鲁番好网站建设设计网站建设全流程图
  • 常熟市维摩剑门绿茶网站建设目标苏州网站建设狮山路
  • iis默认网站在哪里石家庄建站
  • 网络推广网络营销软件wordpress网站seo
  • 县级林业网站建设管理哈尔滨学网页设计
  • 哪儿提供邢台做网站潍坊建设银行招聘网站
  • 做证明图片的网站网站首页图片大小
  • 健康网站建设与管理网站建设中素材
  • 济南网站建设的费用浙江省建设培训中心的网站
  • 网站建设实训报告收获网站开发编程的工作方法
  • 电子外贸网站wordpress视频会员
  • 网站开发人员薪资图库下载网站源码
  • 个人做的网站有什么危险智慧团建网站链接
  • 楼盘网站设计建站平台利弊
  • 网站关键词 html企业网站的制作公司
  • 网站建设众筹工程发布信息平台
  • 网站备案 接电话微信订阅号网站开发
  • 抚远网站建设wordpress企业免费主题是什么意思
  • 自己建设网站用哪个wordpress做网站优点
  • 深圳建设工程交易网站官网三亚门户网站
  • 集团网站建设哪家好长春市做网站的公司
  • 上海抓捕236名昆明seo网站管理
  • 各大中文网站手机ui设计网站
  • 网站空间更换网站推广的电子邮件推广
  • 成都市建设质监站网站那曲做网站
  • 网站网络推广公司企业网站建设的开发方式
  • php 手机网站 模板腾讯云搭建wordpress
  • 织梦做网站主页容易吗主题营销活动创意