
每当对对象调用wait()方法时,它都会导致当前线程等待,直到另一个线程调用notify()或notifyAll( ) 该对象的方法,而 wait(long timeout) 导致当前线程等待,直到另一个线程调用 notify() 或 notifyAll( ) 该对象的方法,或者已过了指定的超时时间。
wait()
在下面的程序中,当 wait() 在对象上调用,线程从运行状态进入等待状态。它等待其他线程调用 notify() 或 notifyAll() 才能进入可运行状态,将会形成死锁。
示例
class MyRunnable implements Runnable {
public void run() {
synchronized(this) {
System.out.println("In run() method");
try {
this.wait();
System.out.println("Thread in waiting state, waiting for some other threads on same object to call notify() or notifyAll()");
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
public class WaitMethodWithoutParameterTest {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable, "Thread-1");
thread.start();
}
}输出
In run() method
采用HttpClient向服务器端action请求数据,当然调用服务器端方法获取数据并不止这一种。WebService也可以为我们提供所需数据,那么什么是webService呢?,它是一种基于SAOP协议的远程调用标准,通过webservice可以将不同操作系统平台,不同语言,不同技术整合到一起。 实现Android与服务器端数据交互,我们在PC机器java客户端中,需要一些库,比如XFire,Axis2,CXF等等来支持访问WebService,但是这些库并不适合我们资源有限的android手机客户端,
wait(long)
在下面的程序中,当对对象调用 wait(1000)时,线程从运行状态进入等待状态,即使在超时时间过后没有调用notify()或notifyAll()线程也会从等待状态进入可运行状态。
示例
class MyRunnable implements Runnable {
public void run() {
synchronized(this) {
System.out.println("In run() method");
try {
this.wait(1000);
System.out.println("Thread in waiting state, waiting for some other threads on same object to call notify() or notifyAll()");
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
public class WaitMethodWithParameterTest {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable, "Thread-1");
thread.start();
}
}输出
In run() method Thread in waiting state, waiting for some other threads on same object to call notify() or notifyAll()









