您的位置:首页 > 编程语言 > Java开发

关于java多线程的几个例子

2008-07-31 14:56 405 查看
转自http://blog.csdn.net/java2000_net/archive/2008/06/15/2551072.aspx

线程间的通信 1. 线程的几种状态 线程有四种状态,任何一个线程肯定处于这四种状态中的一种:

1) 产生(New):线程对象已经产生,但尚未被启动,所以无法执行。如通过new产生了一个线程对象后没对它调用start()函数之前。

2) 可执行(Runnable):每个支持多线程的系统都有一个排程器,排程器会从线程池中选择一个线程并启动它。当一个线程处于可执行状态时,表示它可能正处于线程池中等待排排程器启动它;也可能它已正在执行。如执行了一个线程对象的start()方法后,线程就处于可执行状态,但显而易见的是此时线程不一定正在执行中。

3) 死亡(Dead):当一个线程正常结束,它便处于死亡状态。如一个线程的run()函数执行完毕后线程就进入死亡状态。

4) 停滞(Blocked):当一个线程处于停滞状态时,系统排程器就会忽略它,不对它进行排程。当处于停滞状态的线程重新回到可执行状态时,它有可能重新执行。如通过对一个线程调用wait()函数后,线程就进入停滞状态,只有当再次对该线程调用notify或notifyAll后它才能再次回到可执行状态。

sleep方法不推荐使用,可用wait。 线程退出最好自己实现,在运行状态中一直检验一个状态,如果这个状态为真,就一直运行,如果外界更改了这个状态变量,那么线程就停止运行。

sleep()使当前线程进入停滞状态,所以执行sleep()的线程在指定的时间内肯定不会执行;yield()只是使当前线程重新回到可执行状态,所以执行yield()的线程有可能在进入到可执行状态后马上又被执行。 sleep()可使优先级低的线程得到执行的机会,当然也可以让同优先级和高优先级的线程有执行的机会;yield()只能使同优先级的线程有执行的机会。

当调用wait()后,线程会释放掉它所占有的“锁标志”,从而使线程所在对象中的其它synchronized数据可被别的线程使用。

waite()和notify()因为会对对象的“锁标志”进行操作,所以它们必须在synchronized函数或synchronized block中进行调用。如果在non-synchronized函数或non-synchronized block中进行调用,虽然能编译通过,但在运行时会发生IllegalMonitorStateException的异常。

suspend()、resume() 1) 通过suspend()函数,可使线程进入停滞状态。通过suspend()使线程进入停滞状态后,除非收到resume()消息,否则该线程不会变回可执行状态。 2) 当调用suspend()函数后,线程不会释放它的“锁标志”。

sleep() 1) sleep ()函数有一个参数,通过参数可使线程在指定的时间内进入停滞状态,当指定的时间过后,线程则自动进入可执行状态。 2) 当调用sleep ()函数后,线程不会释放它的“锁标志”。

yield() 1) 通过yield ()函数,可使线程进入可执行状态,排程器从可执行状态的线程中重新进行排程。所以调用了yield()的函数也有可能马上被执行。 2) 当调用yield ()函数后,线程不会释放它的“锁标志”。

sleep()和yield()的区别 1) sleep()使当前线程进入停滞状态,所以执行sleep()的线程在指定的时间内肯定不会执行;yield()只是使当前线程重新回到可执行状态,所以执行yield()的线程有可能在进入到可执行状态后马上又被执行。 2) sleep()可使优先级低的线程得到执行的机会,当然也可以让同优先级和高优先级的线程有执行的机会;yield()只能使同优先级的线程有执行的机会。

join() 使调用join()的线程执行完毕后才能执行其它线程,在一定意义上,它可以实现同步的功能。

wait()、notify()和notifyAll()

1) wait()函数有两种形式:第一种形式接受一个毫秒值,用于在指定时间长度内暂停线程,使线程进入停滞状态。第二种形式为不带参数,代表waite()在notify()或notifyAll()之前会持续停滞。

2) 当对一个对象执行notify()时,会从线程等待池中移走该任意一个线程,并把它放到锁标志等待池中;当对一个对象执行notifyAll()时,会从线程等待池中移走所有该对象的所有线程,并把它们放到锁标志等待池中。

3) 当调用wait()后,线程会释放掉它所占有的“锁标志”,从而使线程所在对象中的其它synchronized数据可被别的线程使用。

wait()、notify()和synchronized

waite()和notify()因为会对对象的“锁标志”进行操作,所以它们必须在synchronized函数或synchronized block中进行调用。如果在non-synchronized函数或non-synchronized block中进行调用,虽然能编译通过,但在运行时会发生IlegalMonitorStateException的异常。

wait()、notify()、notifyAll()和suspend()、resume()、sleep()区别

1) wait()使当前线程进入停滞状态时,还会释放当前线程所占有的“锁标志”,从而使线程对象中的synchronized资源可被对象中别的线程使用;而suspend()和sleep()使当前线程进入停滞状态时不会释放当前线程所占有的“锁标志”。

2) 前一组函数必须在synchronized函数或synchronized block中调用,否则在运行时会产生错误;而后一组函数可以non-synchronized函数和synchronized block中调用。

这两组函数的取舍 Java2已不建议使用后一组函数。因为在调用wait()时不会释放当前线程所取得的“锁标志”,这样很容易造成“死锁”。

例子1

package threadTest;

class TestThreadMethod extends Thread {

public static int shareVar = 0;

public TestThreadMethod(String name) {
super(name);
}

public synchronized void run() {
if (shareVar == 0) {
for (int i = 0; i < 5; i++) {
shareVar++;
if (shareVar == 3) {
this.suspend();
}
}
} else {
System.out.println(Thread.currentThread().getName() + " shareVar = " + shareVar);
this.resume();
}
}
}

public class MyTest {
public static void main(String[] args) {
TestThreadMethod t1 = new TestThreadMethod("t1");
TestThreadMethod t2 = new TestThreadMethod("t2");
t1.start();
t2.start();
}
}

例子2

package threadTest;

class TestThreadMethod2 extends Thread {

public static int shareVar = 0;

public TestThreadMethod2(String name) {
super(name);
}

public synchronized void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println(Thread.currentThread().getName() + " : " + i + " shareVar=" + shareVar);
shareVar++;
}
}
}

public class MyTest2 {
public static void main(String[] args) {
TestThreadMethod2 t1 = new TestThreadMethod2("t1");
TestThreadMethod2 t2 = new TestThreadMethod2("t2");
t1.start();
t2.start();
// new Thread(t1).start();
// new Thread(t1).start();
// new Thread(t2).start();
}
}

例子3

package threadTest;

class TestThreadMethod3 extends Thread {

public static int shareVar = 0;

public TestThreadMethod3(String name) {
super(name);
}

public synchronized void run() {
for (int i = 0; i < 5; i++) {
try {
if (Thread.currentThread().getName().equals("t1"))
Thread.sleep(150);
else
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println(Thread.currentThread().getName() + " : " + i + " shareVar=" + shareVar);
shareVar++;
}
}
}

public class MyTest3 {
public static void main(String[] args) {
TestThreadMethod3 t1 = new TestThreadMethod3("t1");
TestThreadMethod3 t2 = new TestThreadMethod3("t2");
t1.start();
t2.start();
}
}

例子4

package threadTest;

class TestThreadMethod4 extends Thread {

public static int shareVar = 0;

public TestThreadMethod4(String name) {
super(name);
}

public synchronized void run() {
for (int i = 0; i < 5; i++) {
Thread.yield();
System.out.println(Thread.currentThread().getName() + " : " + i + " shareVar=" + shareVar);
shareVar++;
}
}
}

public class MyTest4 {
public static void main(String[] args) {
TestThreadMethod4 t1 = new TestThreadMethod4("t1");
TestThreadMethod4 t2 = new TestThreadMethod4("t2");
t1.start();
t2.start();
// new Thread(t1).start();
// new Thread(t1).start();
// new Thread(t2).start();
}
}

例子5

package threadTest;

class TestThreadMethod5 extends Thread {

public static int shareVar = 0;

public TestThreadMethod5(String name) {
super(name);
}

public void run() {
for (int i = 0; i < 5; i++) {
Thread.yield();
// try {
// Thread.sleep(300);
// } catch (InterruptedException e) {
// System.out.println("Interrupted");
// }
System.out.println(Thread.currentThread().getName() + " : " + i + " shareVar=" + shareVar);
shareVar++;
}
}
}

public class MyTest5 {
public static void main(String[] args) {
TestThreadMethod5 t1 = new TestThreadMethod5("t1");
TestThreadMethod5 t2 = new TestThreadMethod5("t2");
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
}

例子6

package threadTest;

class TestThreadMethod6 extends Thread {

public static int shareVar = 0;

public TestThreadMethod6(String name) {
super(name);
}

public void run() {
for (int i = 0; i < 4; i++) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println(Thread.currentThread().getName() + " : " + i + " shareVar=" + shareVar);
shareVar++;
}
}
}

public class MyTest6 {
public static void main(String[] args) {
TestThreadMethod6 t1 = new TestThreadMethod6("t1");
TestThreadMethod6 t2 = new TestThreadMethod6("t2");
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
}
t2.start();
}
}

例子7

package threadTest;

class TestThreadMethod7 extends Thread {

public static int shareVar = 0;

public TestThreadMethod7(String name) {
super(name);
}

public synchronized void run() {
if (shareVar == 0) {
for (int i = 0; i < 10; i++) {
shareVar++;
if (shareVar == 5) {
try {
this.wait();
} catch (InterruptedException e) {
}
}
}
}
if (shareVar != 0) {
System.out.println(Thread.currentThread().getName() + " shareVar = " + shareVar);
this.notify();
}
}
}

public class MyTest7 {
public static void main(String[] args) {
TestThreadMethod7 t1 = new TestThreadMethod7("t1");
TestThreadMethod7 t2 = new TestThreadMethod7("t2");
new Thread(t1).start();
// new Thread(t1).start();
new Thread(t2).start();
}
}

例子8

package threadTest;

class TestThreadMethod8 extends Thread {

public int shareVar = 0;

public TestThreadMethod8(String name) {
super(name);
new Notifier(this);
}

public synchronized void run() {
if (shareVar == 0) {
for (int i = 0; i < 5; i++) {
shareVar++;
System.out.println("i = " + shareVar);
try {
System.out.println("wait......");
this.wait();
} catch (InterruptedException e) {
}
}
}
}
}

class Notifier extends Thread {
private TestThreadMethod8 ttm;
Notifier(TestThreadMethod8 t) {
ttm = t;
start();
}

public void run() {
while (true) {
try {
sleep(2000);
} catch (InterruptedException e) {
}

/* 1 要同步的不是当前对象的做法 */
synchronized (ttm) {
System.out.println("notify......");
ttm.notify();
}
}
}
}

public class MyTest8 {
public static void main(String[] args) {
TestThreadMethod8 t1 = new TestThreadMethod8("t1");
t1.start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: