isAlive(),join()的使用
isAlive()方法在Thread中定义:final bollean isAlive() ,
所以只能在Thread类的实例或其子类中调用.
一个更经常使用的方法是调用join()方法来等待另一个线程的结束.它的定义如下:
final void join() throws InterruptedException
这个方法一直等待,直到它调用的线程终止.
package mythread;
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
public class DemoJoinIsAlive {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: " + ob1.t.isAlive());
System.out.println("Thread Two is alive: " + ob2.t.isAlive());
System.out.println("Thread Three is alive: " + ob3.t.isAlive());
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: " + ob1.t.isAlive());
System.out.println("Thread Two is alive: " + ob2.t.isAlive());
System.out.println("Thread Three is alive: " + ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}
同步
可以用两种方式实现:使用同步方法;同步语句(同步代码块).它们都使用关键字synchronized.
一.使用同步方法:
直接上代码:
/**
* 演示同步方法的使用!!!
* NOTES:
* java 中每一个对象都有一个隐含锁(或称监控器),
* 记住,一旦一个线程进入一个实例的任何同步方法,别的线程将不能进入同一实例的其他同步方法,
* 因为它没有持有此实例的(隐含)锁,但是该实例的非同步方法仍然能够被调用.
*
*/
class Callme {
//比较下面两行方法的声明,当加synchronized时同步,否则不会同步.
// synchronized void call(String msg) {
void call(String msg) {
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller implements Runnable {
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s) {
target = targ;
msg = s;
t = new Thread(this);
t.start();
}
public void run() {
target.call(msg);
}
}
class Synch {
public static void main(String args[]) {
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
// wait for threads to end
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
}
二.同步语句:
/** NOTES:
* 在类中创建synchronized方法是一种取得同步的简单有效的方法,但它并不是在所有情况下都有效,为什么?
* 考虑下面的情况:假设你想同步访问没有设计为多线程访问的类的对象,也就是说,类不使用synchronized方法,
* 或者更进一步,该类不是由你创建,而是由第三方创建的,你并不能访问它的源代码,因此此时不能在类相应的方法中
* 增加synchronized关键词.那么怎样同步访问该类的对象呢?幸运的是,这个问题的解决方法非常简单:只需要将对
* 该类方法的访问置于一个同步块中.
*/
//This program uses a synchronized block.
class Callme2 {
void call(String msg) {
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller2 implements Runnable {
String msg;
Callme2 target;
Thread t;
public Caller2(Callme2 targ, String s) {
target = targ;
msg = s;
t = new Thread(this);
t.start();
}
// synchronize calls to call()
public void run() {
synchronized (target) { // 同步块 synchronized block
target.call(msg);
}
}
}
public class Synch2 {
public static void main(String args[]) {
Callme2 target = new Callme2();
Caller2 ob1 = new Caller2(target, "Hello");
Caller2 ob2 = new Caller2(target, "Synchronized");
Caller2 ob3 = new Caller2(target, "World");
// wait for threads to end
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
}