多线程实战大总结.docVIP

  • 0
  • 0
  • 约3.91千字
  • 约 8页
  • 2018-01-02 发布于河南
  • 举报
多线程实战大总结

多线程实战大总结 继承Thread的方法: package mmm; public class ThreadText extends Thread { public void run() { for(int i=0;i5;i++){ System.out.println(第一个线程的:+(i+1)+运行); Thread.yield(); } } } package mmm; public class Thread2 extends Thread{ public void run() { for(int i=0;i5;i++){ System.out.println(第二个线程的:+(i+1)+的运行); Thread.yield(); } } } package mmm; public class Text { public static void main(String[] args) { ThreadText th=new ThreadText(); Thread2 thh=new Thread2(); th.start(); thh.start(); } } 实现Runnable的接口的方法: package mmm; public class RunnableText implements Runnable { private int num; @Override public void run() { num++; System.out.println(num); } }package mmm; public class textt { public static void main(String[]args) { RunnableText runn=new RunnableText(); Thread th1=new Thread(runn); th1.start(); Thread th2=new Thread(runn); th2.start(); } } join方法: package mm; public class joinText extends Thread { public joinText(String name) { super(name); } public void run() { for (int i = 0; i 5; i++) { System.out.println(getName() + + i); } } public static void main(String[] args) { for (int i = 0; i 10; i++) { if (i == 5) { joinText jj = new joinText(半路杀出的线程);//阻塞main的线程,你只有等我完成以后才能继续你的线程。 try { jj.start();//启动线程。 jj.join();//当它等于5的时候,我们就去阻塞当前的线程。 } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + + i);//当前线程的main的线程。main方法启动时,就会创建java程序的主线程。 } } } sleep方法: package m; public class sleepText { public static void bysec(long s) { for(int i=0;is;i++) { System.out.println(i+1+秒); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } package m; public class newThread extends Thread{ public void run() { for(int i=0;i20;i++) { System.out.println(i的值为:+i); } } } package m; public class text { public static void main(String[] args

文档评论(0)

1亿VIP精品文档

相关文档