多线程(join和yield)

来源:互联网 发布:js 特殊字符校验 编辑:程序博客网 时间:2024/06/08 11:38
/*
 * 一.join:
 * 当A线程执行到了b线程的join方法时,A线程就会等待,等B现成都执行完,A才会执行。

 * join可一用来临时加入线程执行。


 *二. toString方法:
 * 返回线程的:线程名,优先级,线程组。
 * 优先级:抢资源的频率。
 * 线程可以具有的最高优先级。 
 * static int MAX_PRIORITY (10)   
 * 线程可以具有的最低优先级。 
 *  static int MIN_PRIORITY (1)
 *  分配给线程的默认优先级。 

 * static int NORM_PRIORITY (5)


 *三.static void yield() 
          暂停当前正在执行的线程对象,并执行其他线程。 
 */
class Demo implements Runnable
{
public void run()
{
for(int x=0;x<70;x++)
{
System.out.println(Thread.currentThread().toString()+"----"+x);
Thread.yield();
}
}
}
public class JoinDemo {
public static  void main(String args[]) throws InterruptedException
{
Demo d=new Demo();
Thread t1=new Thread(d);
Thread t2=new Thread(d);
t1.start();
//t1.setPriority(Thread.MAX_PRIORITY);
t2.start();
//t1.join();
for(int x=0;x<80;x++)
{
//System.out.println("main----"+x);
}
System.out.println("over");
}

}

//开发中使用多线程,为了同时运行代码,如:



public class ThreadTest {
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 r=new Runnable()
{
public void run()
{
for(int x=0;x<100;x++)
{
System.out.println(Thread.currentThread().getName()+"---"+x);
}
}
};
new Thread(r).start();
}
}

0 0
原创粉丝点击