企业做网站和宣传册的作用,合肥百度竞价推广代理公司,网站建设pad版本是什么,长安仿做网站后台线程
一个进程中只有后台进程运行#xff0c;该进程将会结束。
新创建的线程默认为前台线程#xff0c;Java中只要有一个前台线程运行#xff0c;就不会结束程序#xff0c;如果只有后台线程运行#xff0c;程序就会结束#xff0c;可以在线程对象启动前执行setDae…后台线程
一个进程中只有后台进程运行该进程将会结束。
新创建的线程默认为前台线程Java中只要有一个前台线程运行就不会结束程序如果只有后台线程运行程序就会结束可以在线程对象启动前执行setDaemon(true)语句设置该线程为后台线程。
class Main {public static void main(String[] args) {System.out.println(main()线程是后台线程吗Thread.currentThread().isDaemon());DaemonThread daemonnew DaemonThread();Thread threadnew Thread(daemon,后台线程);System.out.println(Daemons是后台线程吗thread.isDaemon());thread.setDaemon(true);//设置为后台线程System.out.println(Daemons是后台线程吗thread.isDaemon());thread.start();//模拟主线程main执行int i0;for(;i3;i){System.out.println(i);}}
}class DaemonThread implements Runnable {//模拟分线程执行public void run(){while(true){System.out.println(Thread.currentThread().getName()正在运行);}}} 线程的优先级
使用setPriority方法设置优先级为1到10数字越大优先级越高获得CPU的使用权机会越大。
class Main {public static void main(String[] args) {Thread minPriority new Thread(new maxpriority(),优先级较高线程);Thread maxPrioritynew Thread(new minpriority(),优先级较低线程);minPriority.setPriority(Thread.MIN_PRIORITY);//设置优先级为1maxPriority.setPriority(Thread.MAX_PRIORITY);//设置优先级为10//开启两个线程minPriority.start();maxPriority.start();}}
class maxpriority implements Runnable{public void run(){for(int i0;i3;i) {System.out.println(Thread.currentThread().getName() 正在输出 i);}}}
class minpriority implements Runnable{public void run(){for(int i0;i3;i) {System.out.println(Thread.currentThread().getName() 正在输出 i);}}
} 线程休眠
当前线程暂停运行进入阻塞状态哦使用sleep()方法。传入参数单位为毫秒。
sleep调用时只能让当前正在运行的线程休眠
class Main {public static void main(String[] args) {new Thread(new Sleepthread()).start();//开启新线程for(int i1;i8;i){try{ if(i5){Thread.sleep(1000);}System.out.println(主线程正在输出i);Thread.sleep(500);}catch(InterruptedException e){e.printStackTrace();}}}}
class Sleepthread implements Runnable{public void run(){for(int i1;i8;i){if(i3){try{Thread.sleep(1000);}catch(Exception e){e.printStackTrace();}}System.out.println(Sleep线程正在输出i);try{ Thread.sleep(500);}catch(Exception e){e.printStackTrace();}}}
} 主线程在Sleep线程输出3之前连续输出2,3,4说明此时Sleep线程在输出值3时先休眠了一段时间
另外Sleep线程再输出6之前主线程也连续输出5,6说明此时Sleep在输出6时休眠了一段时间。
线程插队
class Main {public static void main(String[] args) throws Exception {Thread threadnew Thread(new Joinrunable(),thread);thread.start();for(int i1;i4;i){if(i2){thread.join();//调用join()方法插队实现}System.out.println(Thread.currentThread().getName()线程正在输出i);}}
}
class Joinrunable implements Runnable{public void run(){for(int i1;i3;i){System.out.println(Thread.currentThread().getName()线程正在输出i);}}
}
main()线程和thread线程互相争夺CPU使用权然后当i3时候 线程生命周期的六种基本状态
1新建状态
创建一个线程对象后还没有调用start()方法启动之前的状态
2可运行状态
就绪状态调用了start()方法之后进入的状态。
3锁阻塞状态
当一个线程想要获取一个对象锁该aii锁被其他线程持有该线程进入锁阻塞状态。
4无限等待状态
一个线程等待另一个线程执行一个唤醒动作该线程进入的状态。
5计时等待状态
具有指定等待时间的状态一直保持到超时或被唤醒
6)被终止状态
终止运行由于正常退出或者异常没有被捕获而结束。
线程让步
某个特定时间点线程暂停抢夺CPU采用yield()方法实现。
class Main {public static void main(String[] args) throws Exception {//新建两个线程Thread th1new Fieldthread(th1);Thread th2new Fieldthread(th2);
//开启两个线程th1.start();th2.start();}
}
class Fieldthread extends Thread {public Fieldthread(String name) {super(name);//调用父类带参构造方法}public void run() {for(int i1;i4;i){System.out.println(Thread.currentThread().getName()线程输出i);if(i2){System.out.println(线程让步);//线程让步Thread.yield();}}}
} 线程中断
调用两种方法实现
interrupt()方法和isInterrupted()方法
isInterrupted方法判断中断标志位如果为真表示中断。
class Main {public static void main(String[] args) throws Exception {Thread threadnew Thread(new Runnable() {public void run() {for (int i 0; i 4; i) {if(i2){Thread.currentThread().interrupt();System.out.println(线程是否中断Thread.currentThread().isInterrupted());}}}});//创建实例对象thread.start();//启动线程}
} 线程同步
class Main {public static void main(String[] args) throws Exception {Salethread salethread new Salethread();new Thread((salethread),线程1).start();//创建并启动新线程new Thread((salethread),线程2).start();//创建并启动新线程new Thread((salethread),线程3).start();//创建并启动新线程}
}
class Salethread implements Runnable{private int tickets10;public void run(){while(tickets0){try{Thread.sleep(300);}catch(Exception e){e.printStackTrace();}System.out.println(Thread.currentThread().getName()卖出票号是 tickets--);}}
} 结果的票数中出现了0之所以这样是因为每个线程都会先调用sleep方法进入休眠一段时间。即假设票数为3时候线程2先进入while循环然后调用sleepf方法休眠一段时间在此期间票数值不变因为票数只有在最后输出时候才会减少线程3进入while循环然后调用sleep()方法休眠一段时间于此同时然后线程1进入while循环然后调用sleep方法进入休眠最后三个线程依次结束休眠状态相继售票即票数由3变化到0
所以为了安全起见使用同步代码块使得多线程访问处理同一资源时候任何时刻只能由一个线程访问处理。
将共享资源的代码放在synchronizedlock关键字修饰的代码块中。
synchronized(lock){
处理共享资源的代码块
}
lock是指锁即某个线程执行时其他线程不能执行。
class Main {public static void main(String[] args) throws Exception {Salethread salethread new Salethread();new Thread((salethread),线程1).start();//创建并启动新线程new Thread((salethread),线程2).start();//创建并启动新线程new Thread((salethread),线程3).start();//创建并启动新线程}
}
class Salethread implements Runnable{private int tickets10;Object locknew Object();//定义锁public void run(){while(tickets0){synchronized (lock) { try{Thread.sleep(300);}catch(Exception e){e.printStackTrace();}if(tickets0){ System.out.println(Thread.currentThread().getName()卖出票号是 tickets--);}else{break;}}}}
} 同步方法 文章转载自: http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.llsrg.cn.gov.cn.llsrg.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.bfmrq.cn.gov.cn.bfmrq.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.rsmtx.cn.gov.cn.rsmtx.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.qpsxz.cn.gov.cn.qpsxz.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.rybr.cn.gov.cn.rybr.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.yrdn.cn.gov.cn.yrdn.cn http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn http://www.morning.ltqtp.cn.gov.cn.ltqtp.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.ltffk.cn.gov.cn.ltffk.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.krklj.cn.gov.cn.krklj.cn http://www.morning.trhlb.cn.gov.cn.trhlb.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.mhnd.cn.gov.cn.mhnd.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.mymz.cn.gov.cn.mymz.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.wxqmc.cn.gov.cn.wxqmc.cn http://www.morning.lwrcg.cn.gov.cn.lwrcg.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.rbjf.cn.gov.cn.rbjf.cn http://www.morning.kzbpx.cn.gov.cn.kzbpx.cn http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn http://www.morning.qxwrd.cn.gov.cn.qxwrd.cn http://www.morning.tygn.cn.gov.cn.tygn.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.fgtls.cn.gov.cn.fgtls.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.mhpmw.cn.gov.cn.mhpmw.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn http://www.morning.xsszn.cn.gov.cn.xsszn.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.wnbpm.cn.gov.cn.wnbpm.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.gnkdp.cn.gov.cn.gnkdp.cn