线程事件通知 & 线程方法
package ersatz.thread;
public class T {
public static void main(String[] args) throws InterruptedException {
B b = new B();
b.start();
Thread.sleep(5 * 1000);
b.setLoop(false);
}
}
class B extends Thread {
private int n;
private boolean loop = true;
public void setLoop(boolean loop) {
this.loop = loop;
}
@Override
public void run() {
while (loop) {
System.out.println("B " + n++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
interrupt
package ersatz.thread;
public class T {
public static void main(String[] args) throws InterruptedException {
B b = new B();
b.setName("uiop");
b.setPriority(Thread.MIN_PRIORITY);
b.start();
for (int i = 0; i < 5; ++i) {
Thread.sleep(1000);
System.out.println("main thread");
}
System.out.println(b.getName() + " b priority " + b.getPriority());
b.interrupt();
}
}
class B extends Thread {
@Override
public void run() {
while (true) {
for (int i = 0; i < 100; ++i) {
System.out.println(Thread.currentThread().getName() + " i = " + i);
}
try {
System.out.println(Thread.currentThread().getName() + " sleeping");
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " interrupted");
}
}
}
}
join
package ersatz.thread;
public class T {
public static void main(String[] args) throws InterruptedException {
B b = new B();
b.start();
for (int i = 0; i < 8; ++i) {
Thread.sleep(1000);
System.out.println("Main "+i);
if (i == 3) {
System.out.println(b.getClass().getName()+" join");
b.join();
System.out.println(b.getClass().getName()+" join finish");
}
}
}
}
class B extends Thread {
@Override
public void run() {
for (int i = 0; i < 7; ++i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getClass().getName() + ' ' + i);
}
}
}

Thread.yield() 礼让cpu not comple
package ersatz.thread;
public class T {
public static void main(String[] args) throws InterruptedException {
B b = new B();
b.start();
for (int i = 0; i < 8; ++i) {
Thread.sleep(1000);
System.out.println("Main "+i);
if (i == 3) {
System.out.println(b.getClass().getName()+" join");
// b.join();
Thread.yield(); // not compel
System.out.println(b.getClass().getName()+" join finish");
}
}
}
}
class B extends Thread {
@Override
public void run() {
for (int i = 0; i < 7; ++i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getClass().getName() + ' ' + i);
}
}
}
package ersatz.thread;
public class T {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new B());
int c=0;
do {
System.out.println("Main " + ++c);
if (c == 3) {
t.start();
t.join();
}
Thread.sleep(500);
} while (c != 6);
}
}
class B implements Runnable{
private int c;
@Override
public void run() {
do {
System.out.println(this.getClass().getName() + ++c);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (c != 5);
}
}
Daemon Thread
package ersatz.thread;
public class T {
public static void main(String[] args) throws InterruptedException {
DaemonThread daemonThread = new DaemonThread();
daemonThread.setDaemon(true);
daemonThread.start();
int c=0;
do {
System.out.println(T.class + " "+c);
Thread.sleep(500);
} while (c++ != 4);
}
}
class DaemonThread extends Thread{
@Override
public void run() {
for (; ; ) {
System.out.println(this.getName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
查看线程状态
package ersatz.thread;
public class T {
public static void main(String[] args) throws InterruptedException {
B b = new B();
System.out.println(b.getName() + " state: " + b.getState());
b.start();
while (b.getState() != Thread.State.TERMINATED) {
System.out.println(b.getName() + " state: " + b.getState());
Thread.sleep(500);
}
System.out.println(b.getName() + " state: " + b.getState());
}
}
class B extends Thread {
@Override
public void run() {
while (true) {
for (int i = 0; i < 10; ++i) {
System.out.println(this.getClass().getName() + ' ' + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
}
}
}

浙公网安备 33010602011771号