内容发布更新时间 : 2024/12/23 4:13:58星期一 下面是文章的全部内容请认真阅读。
结果如下:
线程运行——>0 线程运行——>1 线程运行——>2 线程运行——>3 线程运行——>4
二、中断线程
class MyThread3 implements Runnable{ public void run(){ System.out.println(\、进入run方法\); try{ Thread.sleep(10000); System.out.println(\、已经完成休眠\); }catch(Exception e){ System.out.println(\、休眠被终止\); return; //让程序返回被调用处 } System.out.println(\、run 方法正常结束\); } }
public class ThreadInterruptDemo { public static void main(String[] args) { MyThread3 mt=new MyThread3(); Thread t=new Thread(mt,\线程\); t.start(); try{ Thread.sleep(2000);//稍微停2s在进行中断 }catch(Exception e){} t.interrupt(); } }
显示结果:
1、进入run方法 3、休眠被终止
三、后台线程。
//在线程MyThread中,尽管run()方法是死循环的方式,但是程序依然可以执行完,因为方法中死循环的线程操作已经设置成后台运行了
class MyThread4 implements Runnable{ public void run(){ while(true){ System.out.println(Thread.currentThread().getName()+\在运行。\); } } }
public class ThreadDaemonDemo { public static void main(String[] args) { MyThread4 mt=new MyThread4(); Thread t=new Thread(mt,\线程1\);
t.setDaemon(true); //此线程在后台运行 t.start(); //启动线程 } }
四、线程A,休眠1秒;线程B,休眠3秒;线程C,休眠4秒,继承Thread类
class MyThread5 extends Thread{ private int time; public MyThread5(String name,int time){ super(name); this.time=time; } public void run(){ try{ Thread.sleep(this.time); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+\线程,休眠\+this.time+\毫秒。\); } }
public class ExecDemo { public static void main(String[] args) { MyThread5 mt1=new MyThread5(\线程A\,1000); MyThread5 mt2=new MyThread5(\线程B\,3000); MyThread5 mt3=new MyThread5(\线程C\,4000); mt1.start(); mt2.start(); mt3.start(); } }
五、如果使用Runnable接口实现多线程,则不像Thread类那样可以直接使用Thread类中的name属性,需要在类中单独定义一个name是想以保存线程名称
class MyThread6 implements Runnable{ private String name; private int time; public MyThread6(String name,int time){ this.name=name; this.time=time; } public void run(){ try{ Thread.sleep(this.time); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+\线程,休眠\+this.time+\毫秒。\); } }
public class ExecDemo1 { public static void main(String[] args) { MyThread6 mt1=new MyThread6(\线程A\,1000);
}
}
MyThread6 mt2=new MyThread6(\线程B\,3000); MyThread6 mt3=new MyThread6(\线程C\,4000); new Thread(mt1).start(); new Thread(mt2).start(); new Thread(mt3).start();
六、解除死锁代码。
class Zhangsan{ public void say(){ System.out.println(\张三对李四说:“你给我画,我就把书给你!”\); } public void get(){ System.out.println(\张三得到画了。\); } }
class Lisi{ public void say(){ System.out.println(\李四对张三说:“你给我书,我就把画给你!”\); } public void get(){ System.out.println(\李四得到书了。\); } }
public class DeadLock implements Runnable{ private static Zhangsan zs=new Zhangsan(); private static Lisi ls=new Lisi(); private boolean flag=false; public void run(){ if(flag){ synchronized (zs){ zs.say(); try{ Thread.sleep(500); }catch(InterruptedException e){ e.printStackTrace(); } synchronized(ls){ zs.get(); } } }else{ synchronized (zs){ ls.say(); try{ Thread.sleep(500); }catch(InterruptedException e){ e.printStackTrace(); } synchronized(ls){ ls.get(); } } } } public static void main(String[] args){
DeadLock t1=new DeadLock(); DeadLock t2=new DeadLock(); t1.flag=true; t2.flag=false; Thread thA=new Thread(t1); Thread thB=new Thread(t2); thA.start(); thB.start(); } }
显示结果:
张三对李四说:“你给我画,我就把书给你!” 张三得到画了。
李四对张三说:“你给我书,我就把画给你!” 李四得到书了。
一、实现Runnable接口可以资源共享
class MyThread implements Runnable{ private int ticket=5; public void run(){ for(int i=0;i<100;i++){ if(ticket>0){ System.out.println(\卖票:ticket=\+ticket--); } } } }
public class RunnableDemo1 { public static void main(String[] args) { MyThread my=new MyThread(); new Thread(my).start(); new Thread(my).start(); new Thread(my).start(); } }
结果显示不止下列三个:
卖票:ticket=5 卖票:ticket=3 卖票:ticket=4 卖票:ticket=1 卖票:ticket=2 卖票:ticket=3 卖票:ticket=4 卖票:ticket=1 卖票:ticket=2 卖票:ticket=5 卖票:ticket=3 卖票:ticket=2 卖票:ticket=1 卖票:ticket=4 卖票:ticket=5
二、观察程序的输出
class MyThread1 implements Runnable{ }
public class CurrentThreadDemo1 { }
//在java中所有的线程都是同时启动的,哪个线程先抢到CPU资源,哪个线程就先运行,所以显示结果不唯一
public static void main(String[] args) { }
MyThread1 my =new MyThread1(); new Thread(my,\线程\).start(); public void run(){ }
for(int i=0;i<3;i++){ }
System.out.println(Thread.currentThread().getName()+\运行,
i=\+i);
my.run();
三、线程的强制执行,用join()方法,线程强制执行期间,其他线
程无法运行,必须等到线程完成之后才可以继续执行。
class MyThread1 implements Runnable{ }
public class ThreadJoinDemo {
public static void main(String[] args) {
MyThread1 mt=new MyThread1(); Thread t=new Thread(mt,\线程\); public void run(){ }
for(int i=0;i<8;i++){ }
System.out.println(Thread.currentThread().getName()+\运行
——>\+i);
t.start();
for(int i=0;i<8;i++){ if(i>4){ }
try{
t.join();
}catch(Exception e){}