$B%9%l%C%I$NM%@hEY$N@_Dj(B

Revised: Mar./23rd/2002

$B%9%l%C%I$OM%@hEY$d3+;O=g=x$K4p$E$$$F!"%9%1%8%e!<%i$K$h$C$Fe$2$?$j2<$2$?$j$9$k$3$H$G!"%9%l%C%I$N

public final void setPriority(int newPriority)
public final int getPriority()

$BM%@hEY$r@_Dj!?sleep()/yield() $B$H$O0[$J$j!"(B static $B@k8@$5$l$F$$$J$$IaDL$N%$%s%9%?%s%9!&%a%=%C%I$G$9!#$7$?$,$C$F!"%$%s%9%?%s%92=$7$?%*%V%8%'%/%H8GM-$N%a%=%C%I$H$7$F8F$S=P$7$^$9!#(B

$B$3$3$G@_Dj$G$-$kM%@hEY$O(B 1 $B$+$i(B 10 $B$^$G$N@0?tCM$G!"?t;z$,Bg$-$$J}$,M%@h$7$F

MIN_PRIORITY
public static final int MIN_PRIORITY
$BCM$O#1!#%9%l%C%I$K@_Dj$G$-$k:GDcM%@h=g0L$G$9!#(B
NORM_PRIORITY
public static final int NORM_PRIORITY
$BCM$O#5!%%9%l%C%I$K3d$jEv$F$i$l$k%G%U%)%k%H$NM%@h=g0L$G$9!#(B
MAX_PRIORITY
public static final int MAX_PRIORITY
$BCM$O#1#0!%%9%l%C%I$K@_Dj$G$-$k:G9bM%@h=g0L$G$9!#(B

$B$3$l$i$NDj?t$O(B static $B@k8@$5$l$F$$$k%/%i%9JQ?t$J$N$G!"MxMQ$9$k>l9g$O(B Thread $B%/%i%97?$N%U%#!<%k%I$H$7$F%"%/%;%9$7$^$9!#L^O@!";2>H85$N%/%i%9$,(B Thread $B%/%i%9$r7Q>5$7$?%5%V%/%i%9$N>l9g$O!"@\F,<-$r$D$1$J$/$F$bD>@\MxMQ$G$-$^$9!#(B

// $BgetPriority();
pri.setPriority(3);
pri.setPriority(Thread.MAX_PRIORITY);

PriorityTest.java:

class PrioritizingA implements Runnable {
	public void run() {
		System.out.println("PrioritizingA Start");
		System.out.println("PrioritizingA End");
	}
}

class PrioritizingB implements Runnable {
	public void run() {
		System.out.println("\t" + "PrioritizingB Start");
		System.out.println("\t" + "PrioritizingB End");
	}
}

class PriorityTest extends Thread {
	public static void main(String[] args) {
		PriorityTest main = new PriorityTest();
		main.start();
	}

	public void run() {
		System.out.println("main Start");

	// $By(B
		Thread threA = new Thread(runA);
		Thread threB = new Thread(runB);

		System.out.println("");
	// $BM%@hEY$N=PNO(B
		System.out.println("$B%G%U%)%k%H$NM%@hEY(B");
		System.out.println(" main: " + this.getPriority());
		System.out.println("threA: " + threA.getPriority());
		System.out.println("threB: " + threB.getPriority());
		System.out.println("");
	// $BM%@hEY$N%;%C%H(B
		this.setPriority(Thread.MIN_PRIORITY);
		threA.setPriority(Thread.NORM_PRIORITY);
		threB.setPriority(Thread.MAX_PRIORITY);
	// $BM%@hEY$N=PNO(B
		System.out.println("$BL@<(E*$K%;%C%H$7$?M%@hEY(B");
		System.out.println(" main: " + this.getPriority());
		System.out.println("threA: " + threA.getPriority());
		System.out.println("threB: " + threB.getPriority());

		System.out.println("");
	// $B%9%l%C%I$N3+;O(B
		System.out.println("threA $BuBV(B");
		threA.start();
		System.out.println("threB $BuBV(B");
		threB.start();

		System.out.println("main End");
	}
}

$B$3$3$G$O!"%$%s%9%?%s%9%a%=%C%I$G$"$k(B getPriotiry()/setPriority() $B$rMQ$$$k0Y$K!"(B main() $B%a%=%C%I$N%/%i%9$r(B Thread $B$N7Q>5$H$7$F:n$j!"<+J,<+?H$r%$%s%9%?%s%92=$7$F$$$^$9!#(B

C:\Java\Thread>javac PriorityTest.java

C:\Java\Thread>java PriorityTest
main Start

$B%G%U%)%k%H$NM%@hEY(B
 main: 5
threA: 5
threB: 5

$BL@<(E*$K%;%C%H$7$?M%@hEY(B
 main: 1
threA: 5
threB: 10

threA $BuBV(B
PrioritizingA Start
PrioritizingA End
threB $BuBV(B
        PrioritizingB Start
        PrioritizingB End
main End

C:\Java\Thread>

$BM%@hEY$r@_Dj$7$?3d$K$O!"$"$^$j7`E*$JJQ2=$O$"$j$^$;$s!#M%@hEY$NCM$,%9%1%8%e!<%j%s%0$KM?$($k1F6A$O!"%W%i%C%H%U%)!<%`$K0lG$$5$l$F$*$j!"8@8l;EMM$H$7$F$O7h$^$C$F$$$^$;$s!#(B

$B$=$l$G$b!"pJs$O2!$5$($F$*$$$?J}$,NI$$$G$7$g$&!'(B



Copyright © 2002 SUGAI, Manabu. All Rights Reserved.
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送