Java多线程之线程调度(二)

(一)线程优先级

线程优先级用1~10表示,10表示优先级最高,默认值是5.每个优先级对应一个Thread类的公用静态常量。如

    public static final int MIN_PRIORITY = 1;
    public static final int NORM_PRIORITY = 5;
    public static final int MAX_PRIORITY = 10;

每个线程的优先级都介于Thread.MIN_PRIORITY和Thread.MAX_PRIORITY之间。

线程的优先级可以通过setPriority(int grade)方法更改,此方法的参数表示要设置的优先级,必须为1~10的整数。

(二)实现线程调度的方法

1,join()方法

使用join方法阻塞线程

/**
 * 
 */
package zut.edu.cs.network.practice;

/**
 * @author Administrator
 *
 */
public class MnThread extends Thread {
	String name;

	/**
	 * @param name
	 */
	public MnThread(String name) {
		super();
		this.name = name;
	}
	@Override
	public void run() {
			for(int i=0;i<5;i++) {
				System.out.println(Thread.currentThread().getName()+" "+i);
			}
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(int i=0;i<10;i++) {
			if(i==5) {
				MnThread t1=new MnThread("thead");
				
				try {
					t1.start();
					t1.join();   //把线程通过join方法插到主线程前面
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println(Thread.currentThread().getName()+" "+i);
		}
	}

}

运行结果:

通过join方法实现两个线程之间的数据传递

/**
 * 
 */
package zut.edu.cs.network.practice;

/**
 * @author Administrator
 *
 */
public class MnThread extends Thread {
	String value;

	@Override
	public void run() {
	value="value 已经赋值";
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MnThread t1=new MnThread();
		t1.start();
		System.out.println("没调用join前:value="+t1.value);
		try {
			t1.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("调用join后:value="+t1.value);
	}

}

程序截图:

2,sleep()方法

使用sleep()方法阻塞线程

/**
 * 
 */
package zut.edu.cs.network.practice;

/**
 * @author Administrator
 *
 */
public class MyThread  {

	public static void bySec(long s) {
		for (int i=0;i<s;i++) {
			System.out.println(i+1+"second");
			try {
				Thread.sleep(1000);//睡眠一秒
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("wait");
		MyThread.bySec(5);
		System.out.println("start");
		}

}

程序运行截图:

3,yield() 方法

yield 的方法可以让当前程序暂停,允许其他程序执行,但该线程仍处于可运行状态,不变为阻塞状态。此时,系统选择优先级相同或更高的线程执行,若无形同或更高的优先级,则该线程继续执行。

使用yield方法暂停程序

/**
 * 
 */
package zut.edu.cs.network.practice;

/**
 * @author Administrator
 *
 */
public class MnThread extends Thread {
	String name ;

	/**
	 * @param name
	 */
	public MnThread(String name) {
		super();
		this.name = name;
	}





	@Override
	public void run() {
	for(int i=0;i<5;i++) {
		System.out.println(Thread.currentThread().getName()+"第"+i+"次运行");
		Thread.yield();
	}
}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MnThread t1=new MnThread("0");
		t1.start();
		MnThread t2=new MnThread("1");
		t2.start();
	}
}

程序运行截图

在例子中,调用了yield方法之后,当前程序并不是进入了阻塞状态,而是继续与其他等待执行的线程竞争cpu资源。

sleep() 与yield()方法的区别:

yield和sleep的主要是,yield方法会临时暂停当前正在执行的线程,来让有同样优先级的正在等待的线程有机会执行。如果没有正在等待的线程,或者所有正在等待的线程的优先级都比较低,那么该线程会继续运行。执行了yield方法的线程什么时候会继续运行由线程调度器来决定,不同的厂商可能有不同的行为。yield方法不保证当前的线程会暂停或者停止,但是可以保证当前线程在调用yield方法时会放弃CPU。

猜你喜欢

转载自blog.csdn.net/qq_41216392/article/details/84704237