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

重庆学校网站建设网站做好了 怎么做解析

重庆学校网站建设,网站做好了 怎么做解析,赫章网站建设,百度品牌推广介绍一下线程中基本的方法使用 线程睡眠sleep() Thread.sleep(毫秒);我们可以通过sleep方法设置让线程睡眠。可以看到sleep是个静态方法 public static native void sleep(long var0) throws InterruptedException; try {System.out.println(new Date().getSeconds());Thread.s…介绍一下线程中基本的方法使用 线程睡眠sleep() Thread.sleep(毫秒);我们可以通过sleep方法设置让线程睡眠。可以看到sleep是个静态方法 public static native void sleep(long var0) throws InterruptedException; try {System.out.println(new Date().getSeconds());Thread.sleep(5000);System.out.println(new Date().getSeconds());} catch (InterruptedException e) {e.printStackTrace();}setDaemon守护线程 非守护线程停止那么守护线程自动退出 public static void main(String[] args) {Thread thread1 new Thread() {Overridepublic void run() {super.run();for(int i 0; i 5; i ) {System.out.println(非守护线程);}}};Thread thread2 new Thread() {Overridepublic void run() {for(int i 0; i 200; i ) {System.out.println(守护线程);}}};thread2.setDaemon(true);thread1.start();thread2.start();}可以很明显的看到thread2本应该执行200次输出但是这里只输出了几行。因为当thread1执行完毕后thread2作为守护线程就自动停止了。 多线程join 如果执行了join方法那么停止当前线程先跑执行了join()的线程。相当于插队执行。如下在执行thread2线程的时候如果i20的时候则让thread1插队先执行 public static void main(String[] args) {final Thread thread1 new Thread() {Overridepublic void run() {super.run();for(int i 0; i 500; i ) {System.out.println(thread1--- i);}}};Thread thread2 new Thread() {Overridepublic void run() {for(int i 0; i 200; i ) {if (i 20) {try {//插队执行thread1.join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(i);}}};thread1.start();thread2.start();} join()方法也可以传参数long 毫秒 join(毫秒) 表示让执行join的线程插队执行XXX毫秒过了时间后两个线程交替执行 public static void main(String[] args) {final Thread thread1 new Thread() {Overridepublic void run() {super.run();for(int i 0; i 500; i ) {System.out.println(thread1--- i);}}};Thread thread2 new Thread() {Overridepublic void run() {for(int i 0; i 200; i ) {if (i 20) {try {//插队执行毫秒thread1.join(1);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(i);}}};thread1.start();thread2.start();} yeild 礼让线程 yeild会让出cpu,让其他线程执行 public static void main(String[] args) {final Thread thread1 new Thread() {Overridepublic void run() {super.run();for(int i 0; i 500; i ) {System.out.println( getName() --- i);}}};Thread thread2 new Thread() {Overridepublic void run() {for(int i 0; i 200; i ) {if (i % 5 0) {Thread.yield();}System.out.println(getName() --- i);}}};thread1.start();thread2.start();} setPriority给线程设置优先级 默认优先级是5 最小最大10 越大优先级越高 public static void main(String[] args) {final Thread thread1 new Thread() {Overridepublic void run() {super.run();for(int i 0; i 500; i ) {System.out.println( getName() --- i);}}};Thread thread2 new Thread() {Overridepublic void run() {for(int i 0; i 500; i ) {System.out.println(getName() --- i);}}};//设置最大的线程优先级最大为10thread1.setPriority(Thread.MIN_PRIORITY);//设置最小的线程优先级最小为thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();} synchronized 同步代码块 当多线程并发多段代码同时执行的时候。希望在执行其中代码的时候cpu不切换线程 不用synchronized的情况 我们来看一下不用synchronized的情况会发生什么 public class ThreadSynchronied {public static void main(String[] args) {final Say say new Say();Thread thread1 new Thread() {Overridepublic void run() {for (int i 0 ; i 10000 ; i ) {say.say();}}};Thread thread2 new Thread() {Overridepublic void run() {for (int i 0 ; i 10000 ; i ) {say.say1();}}};//设置最大的线程优先级最大为10thread1.setPriority(Thread.MIN_PRIORITY);//设置最小的线程优先级最小为thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();} }class Say {void say() {System.out.print(s );System.out.print(a );System.out.print(y );System.out.print(h );System.out.print(e );System.out.print(l );System.out.print(l );System.out.println(o);}void say1() {System.out.print(1 );System.out.print(2 );System.out.print(3 );System.out.print(4 );System.out.print(5 );System.out.print(6 );System.out.print(7 );System.out.println(8);} } 我们发现有些输出并没有打印全在执行线程thread1的过程中cpu被thread2抢占。这种情况下肯定是不符合我们的业务逻辑的。所以我们要保证线程执行了一个完整的方法后cpu才会被其他线程抢占 使用synchronized public class ThreadSynchronied {public static void main(String[] args) {final Say say new Say();Thread thread1 new Thread() {Overridepublic void run() {for (int i 0 ; i 10000 ; i ) {say.say();}}};Thread thread2 new Thread() {Overridepublic void run() {for (int i 0 ; i 10000 ; i ) {say.say1();}}};//设置最大的线程优先级最大为10thread1.setPriority(Thread.MIN_PRIORITY);//设置最小的线程优先级最小为thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();} }class Say {String s hahaah;void say() {synchronized (s) {System.out.print(s );System.out.print(a );System.out.print(y );System.out.print(h );System.out.print(e );System.out.print(l );System.out.print(l );System.out.println(o);}}void say1() {synchronized (s) {System.out.print(1 );System.out.print(2 );System.out.print(3 );System.out.print(4 );System.out.print(5 );System.out.print(6 );System.out.print(7 );System.out.println(8);}} }使用synchronized同步代码块后就发现不会出现上述情况了 同步方法 public class ThreadSynchroniedMethod {public static void main(String[] args) {final Say say new Say();Thread thread1 new Thread() {Overridepublic void run() {for (int i 0 ; i 10000 ; i ) {say.say();}}};Thread thread2 new Thread() {Overridepublic void run() {for (int i 0 ; i 10000 ; i ) {say.say1();}}};//设置最大的线程优先级最大为10thread1.setPriority(Thread.MIN_PRIORITY);//设置最小的线程优先级最小为thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();} }class Say {//在方法上加锁static synchronized void say() {System.out.print(s );System.out.print(a );System.out.print(y );System.out.print(h );System.out.print(e );System.out.print(l );System.out.print(l );System.out.println(o);}static void say1() {synchronized (Say.class) {System.out.print(1 );System.out.print(2 );System.out.print(3 );System.out.print(4 );System.out.print(5 );System.out.print(6 );System.out.print(7 );System.out.println(8);}} }同步方法指的就是在方法上加锁 静态同步方法的所对象是该类的字节码对象 非静态的同步方法锁对象是this 多个线程使用同一资源锁容易造成死锁 什么是死锁? 死锁是指两个或两个以上的进程在执行过程中由于竞争资源或者由于彼此通信而造成的一种阻塞的现象若无外力作用它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁这些永远在互相等待的进程称为死锁进程。 线程安全类 Vector StringBuffer HashTable 线程不安全 ArrayList StringBuilder HashSet java.util.Collections中有synchronizedList等方法支持我们把线程不安全的集合转成线程安全的 学习笔记 多次启动一个线程是非法的 转载于:https://www.cnblogs.com/amberbar/p/9696440.html
http://www.pierceye.com/news/87777/

相关文章:

  • dw旅游网站模板下载外贸网站使用什么品牌国外主机
  • 重庆网站推广流程英国帮人做设计作业网站
  • html5模板网站中国机械加工网1717
  • 上海网站建设知识 博客网络空间设计方案
  • 网站建设宁夏凤凰云深圳多语言网站建设
  • 北京网站备案核验单企业网站维护合同
  • 温州网站建设设计开发网站需要多少资金
  • 网站里添加百度地图网富公司收费大概多少钱
  • 深圳网站建设app开发如何做自己网站
  • 中国移动网站建设情况分析广州建设企业网站
  • 特色设计网站推荐陕西正天建设有限公司网站
  • 泰安定制网站建设公司网络广告设计案例
  • 求2021没封的良心网站门户网站整站源码
  • 做购物平台网站需要注意什么旅游网站页面设计
  • 租用网站如何制作网页纪检监察机关网站建设方案
  • 个人网站建设完整教程怎么做卖车网站
  • 北京定制网站价格竞价恶意点击报案
  • 网站建设策划书 范文怎么做查成绩网站
  • 展厅设计制作网站网站访客qq系统
  • 杨小刀网站建设北京工程造价信息网
  • 个人公司注册流程及材料车辆优化管理专业网站
  • 网站建设怎么上传数据人力资源公司属于什么行业类别
  • 高端企业网站报价昆明优化广告公司
  • 网页设计如何居中郑州纯手工seo
  • 四川建设网站信息查询中心php多语言网站开发
  • 广州建网站腾虎营销网站方案设计
  • zencart网站地图插件写网站建设需求文档
  • 代码自动生成器关键词优化的内容
  • 手机网站建设的图片标书制作员工作内容
  • 阿里巴巴国际站网页版徐闻住房与城乡建设局网站