黑马程序员——线程中join的作用

package com.thread;
 
/*
 * jion可以临时加入一个线程,并且该线程运算完,其他线程继续运行。
 * jion:当a线程执行到了b线程的.jion()方法是,a就会等b线程执行完
 * 才会执行
 * jion可以用来临时加入线程执行。
 * 
 * 
 * yield暂停当前正在执行的线程对象,并执行其他线程
 * 
 * */
public class Note1 implements Runnable {
public void run() {
for (int x = 0; x < 79; x++) {
System.out.println(Thread.currentThread().getName() + x);
Thread.yield();
}
}
}
 class JoinDemo {
public static void main(String[] args) {
Note1 d = new Note1();
Thread t1=new Thread(d);
Thread t2=new Thread(d);
t1.start();
t1.setPriority(Thread.MAX_PRIORITY);
t2.start();
for (int x = 0; x < 80; x++) {
System.out.println("main....." + x);
}
System.out.println("over");
}
}
 
------------------------------------------------------------------
/*
 * 一般开发写线程书写格式
 * 
 * */
 
public class Note1 {
public static void main(String[] args) {
new Thread() {
public void run() {
for (int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName()
+ "......" + x);
}
}
}.start();
 
for (int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName() + "......" + x);
}
Runnable runnable=new Runnable() {
 
@Override
public void run() {
// TODO Auto-generated method stub
for (int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName() + "......" + x);
}
}
};
new Thread(runnable).start();
}
}
 
posted @ 2014-09-07 00:06  大成哥  阅读(146)  评论(0编辑  收藏  举报