Java 线程操作的相关方法
2012-10-10 21:37
507 查看
//线程的相关方法 //1,yield(礼让) 和 join(强制运行) /*class MyThread7 implements Runnable{ public void run(){ for(int i=0; i<5; i++){ System.out.println(Thread.currentThread().getName()+"运行-->"+i); if(i == 2){ System.out.println("线程礼让:"); Thread.currentThread().yield();//线程礼让 } } } } public class TestThread2{ public static void main(String []args){ MyThread7 my = new MyThread7(); Thread t1 = new Thread(my,"线程A"); Thread t2 = new Thread(my,"线程B"); t1.start(); t2.start(); } }*/ /*class MyThread7 implements Runnable{ public void run(){ for(int i=0; i<50; i++){ System.out.println(Thread.currentThread().getName()+"-->"+i); } } } public class TestThread2{ public static void main(String []args){ MyThread7 my = new MyThread7(); Thread t = new Thread(my,"线程A"); t.start(); for(int i=0; i<50; i++){ if(i>25){ try{ t.join(); }catch(Exception e){} } System.out.println("Main线程运行-->"+i); } } }*/ /* 两者的使用方法不同 yield Thread.currentThread().yield(); join t.join(); 要捕捉异常 */ //2,sleep(睡眠) 和 interrupt(中断) /*class MyThread7 implements Runnable{ public void run(){ for(int i=0; i<5; i++){ try{ Thread.sleep(500);//达到延迟操作的效果 }catch(Exception e){} System.out.println(Thread.currentThread().getName()+"-->"+i); } } } public class TestThread2{ public static void main(String []args){ MyThread7 my = new MyThread7(); Thread t = new Thread(my,"线程A"); t.start(); } }*/ class MyThread7 implements Runnable{ public void run(){ System.out.println("1,进入run方法"); try{ Thread.sleep(10000); System.out.println("2,已完成休眠"); }catch(Exception e){ System.out.println("3,休眠被终止"); return; } System.out.println("4,run方法正常结束"); } } public class TestThread2{ public static void main(String []args){ MyThread7 my = new MyThread7(); Thread t = new Thread(my,"线程A"); t.start(); try{ Thread.sleep(2000); }catch(Exception e){} t.interrupt(); } } /* 休眠多久,即是间隔多久 */
相关文章推荐
- Java并发09:Thread的基本方法(6)-线程优先级priority相关说明与操作
- java 线程相关主要方法
- 黑马程序员--Java基础加强--14.利用反射操作泛型III【解析关于泛型类型的细节信息的获取方法】【Method与泛型相关的方法】【个人总结】
- java 多线程6 不同线程操作不同的任务 用r.wait()和r.notify()方法
- java线程相关的方法
- Java中利用JCOM实现操作Excel 相关方法解释
- java web 中判断方法是否运行超时以及运行超时后的再次调用(用到了线程相关的知识)
- Java线程的start方法回调run方法的操作技巧
- java 总结4种线程中操作UI界面的方法
- 谈谈 Java 线程状态相关的几个方法
- Java线程的相关方法串联线程知识
- Java线程的相关方法详细解析
- Java线程的相关方法
- CompressUtils相关代码,java中文件压缩或解压已经文件的基本操作等方法
- java文件操作相关方法
- Android JNI开发高级篇有关Android JNI开发中比较强大和有用的功能就是从JNI层创建、构造Java的类或执行Java层的方法获取属性等操作。 一、类的相关操作 1. jclass FindClass(JNIEnv *env, const char *name);
- Java中的线程(二)-线程相关的常用方法介绍
- 【搞懂Java多线程之一】多线程相关概念,线程生命周期以及线程创建方法
- Java线程相关的常用方法
- 多线程——线程的创建以及操作线程的相关方法简介