做拼团网站关键词搜索排名怎么查看
web/
2025/10/6 9:31:20/
文章来源:
做拼团网站,关键词搜索排名怎么查看,网站建立公司 优帮云,网站如何加入流量统计方法1#xff1a;
1、常见Runnable对象设置同步代码run运行体
class AutoSaleTicket implements Runnable {private int ticket 20;public void run() {while (true) {// 循环是指线程不停的去卖票// 当操作的是共享数据时,用同步代码块进行包围起来,这样在执行时,只能有一…方法1
1、常见Runnable对象设置同步代码run运行体
class AutoSaleTicket implements Runnable {private int ticket 20;public void run() {while (true) {// 循环是指线程不停的去卖票// 当操作的是共享数据时,用同步代码块进行包围起来,这样在执行时,只能有一个线程执行同步代码块里面的内容synchronized (this) {if (ticket 0) {// 不要在同步代码块里面sleep,作用只是自已不执行,也不让线程执行System.out.println(lgqThread.currentThread().getName() 卖出 第 (20 - ticket 1) 张票);ticket--;} else {break;}}// 所以把sleep放到同步代码块的外面,这样卖完一张票就休息一会,让其他线程再卖,这样所有的线程都可以卖票try {Thread.sleep(200);} catch (Exception ex) {}}}
}
2、创建多线程启动多线程
AutoSaleTicket ticket new AutoSaleTicket();
Thread t1 new Thread(ticket, 11东城代售);
Thread t2 new Thread(ticket, 22西城代售);
Thread t3 new Thread(ticket, 33朝阳代售);
Thread t4 new Thread(ticket, 44海淀代售);
t1.start();
t2.start();
t3.start();
t4.start();
3、多线程同步执行结果
03-22 15:40:43.167 9967-10272/com.tianxin.httpheader I/System.out: lgq11东城代售 卖出 第 1 张票 03-22 15:40:43.167 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝阳代售 卖出 第 2 张票 03-22 15:40:43.167 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 卖出 第 3 张票 03-22 15:40:43.167 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 卖出 第 4 张票 03-22 15:40:43.368 9967-10272/com.tianxin.httpheader I/System.out: lgq11东城代售 卖出 第 5 张票 03-22 15:40:43.370 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 卖出 第 6 张票 03-22 15:40:43.370 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝阳代售 卖出 第 7 张票 03-22 15:40:43.371 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 卖出 第 8 张票 03-22 15:40:43.570 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 卖出 第 9 张票 03-22 15:40:43.571 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 卖出 第 10 张票 03-22 15:40:43.572 9967-10272/com.tianxin.httpheader I/System.out: lgq11东城代售 卖出 第 11 张票 03-22 15:40:43.572 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝阳代售 卖出 第 12 张票 03-22 15:40:43.771 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 卖出 第 13 张票 03-22 15:40:43.772 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 卖出 第 14 张票 03-22 15:40:43.773 9967-10272/com.tianxin.httpheader I/System.out: lgq11东城代售 卖出 第 15 张票 03-22 15:40:43.773 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝阳代售 卖出 第 16 张票 03-22 15:40:43.973 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 卖出 第 17 张票 03-22 15:40:43.973 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 卖出 第 18 张票 03-22 15:40:43.974 9967-10272/com.tianxin.httpheader I/System.out: lgq11东城代售 卖出 第 19 张票 03-22 15:40:43.974 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝阳代售 卖出 第 20 张票 方法2线程池使用
创建
class MyTask implements Runnable {private int taskNum;public MyTask(int num) {this.taskNum num;}Overridepublic void run() {System.out.println(lgq正在执行task taskNum);try {Thread.currentThread().sleep(4000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(lgqtask taskNum执行完毕);}
}
执行
ThreadPoolExecutor executor new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,new ArrayBlockingQueueRunnable(5));for(int i0;i15;i){MyTask myTask new MyTask(i);executor.execute(myTask);System.out.println(lgq线程池中线程数目executor.getPoolSize()队列中等待执行的任务数目executor.getQueue().size()已执行玩别的任务数目executor.getCompletedTaskCount());
}
executor.shutdown();
结果
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/87836.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!