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

做网站需要理解什么steam交易链接在哪

做网站需要理解什么,steam交易链接在哪,亦庄建站推广,北京建设信源公司网站并发和并行 并发: 在同一时刻,多个指令在单一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/271280/

相关文章:

  • 淘客那些网站怎么做的建网站必须要服务器吗
  • 滕州市建设局网站精美个人主页
  • 许昌做网站公司汉狮价格注册网站会员违法
  • 建网站的免费空间成都网站建设爱特通
  • 网站设计开发方案WordPress的黑色框
  • 网站设计是什么投资建设一个网站多少钱
  • 网站界面尺寸浏览器打开网站404
  • 网站集约化建设进度汇报南京做网站好的公司
  • 广告联盟点击赚钱平台什么是优化问题
  • 厦门小微企业网站建设补贴南漳网站开发
  • 怎么做外贸网站推广一元友情链接平台
  • 深圳做网站推广公司娄底网站建设的话术
  • 免费网站建设多少钱短视频投放方案
  • 触摸屏网站建设建设银行网站首页打
  • 做个网站需要多钱网络营销方式有哪些?举例说明
  • 优秀网站制作资阳优化团队信息
  • 小程序就是做网站安徽网站seo公司
  • 搜索网站老是跳出别的网站要怎么做asp添加网站管理员
  • 苏州产品网站建设wordpress wp_register_script
  • 永久免费自助建网站我想做个网站
  • 小白自己做网站wordpress大学模板2.7
  • 蚌埠做网站哪家好广告宣传片制作公司
  • 每月网站开发费用东营房产信息网
  • 做网页兼职的网站怎么制作自己的网页
  • 网站租用价格吴镇宇做的电影教学网站
  • 北京自己怎么做网站石家庄西晨网站开发
  • 网站建设技术合同模板下载企业产品营销策划推广
  • wordpress模板 多梦seo网站程序
  • 网站建设责任分解免费的黄冈网站有哪些平台
  • 网站模板用什么做wordpress mip改造