1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
public class ThreadMethod { public static void main(String[] args) { thread t1 = new thread("0"); t1.setName("A"); t1.start();
System.out.println("t1.isAlive() = " + t1.isAlive());
Thread.currentThread().setName("主线程"); System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName()); System.out.println("Thread.currentThread().getPriority() = " + Thread.currentThread().getPriority()); Thread.currentThread().setPriority(Thread.MAX_PRIORITY); System.out.println("Thread.currentThread().getPriority() = " + Thread.currentThread().getPriority());
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
System.out.println(Thread.currentThread().getName() + ":i = " + i); } if (i == 20) { try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } System.out.println("t1.isAlive() = " + t1.isAlive()); } }
class thread extends Thread {
@Override public void run() { for (int i = 0; i < 100; i++) { if (i % 2 != 0) { System.out.println(Thread.currentThread().getName() + ":i = " + i); } if (i % 10 == 0) { Thread.currentThread().yield(); } } }
public thread(String name) { super(name); } }
|