java 线程控制_Java线程控制

Java提供对多线程程序的完全控制。可以开发一个多线程程序,根据要求完全暂停,恢复或停止。可以在线程对象上使用各种静态方法来控制它们的行为。下表列出了一些常用的方法 -

编号

方法

1

public void suspend()

将线程置于挂起状态,并可以使用resume()方法恢复。

2

public void stop()

完全停止线程。

3

public void resume()

恢复使用suspend()方法挂起的线程。

4

public void wait()

导致当前线程等待,直到另一个线程调用notify()。

5

public void notify()

唤醒正在此对象监视器上等待的单个线程。

请注意,最新版本的Java已弃用suspend(),resume()和stop()方法,因此需要使用可用的替代方法。

示例class RunnableDemo implements Runnable {

public Thread t;

private String threadName;

boolean suspended = false;

RunnableDemo( String name) {

threadName = name;

System.out.println("Creating " + threadName );

}

public void run() {

System.out.println("Running " + threadName );

try {

for(int i = 10; i > 0; i--) {

System.out.println("Thread: " + threadName + ", " + i);

// Let the thread sleep for a while.

Thread.sleep(300);

synchronized(this) {

while(suspended) {

wait();

}

}

}

} catch (InterruptedException e) {

System.out.println("Thread " + threadName + " interrupted.");

}

System.out.println("Thread " + threadName + " exiting.");

}

public void start () {

System.out.println("Starting " + threadName );

if (t == null) {

t = new Thread (this, threadName);

t.start ();

}

}

void suspend() {

suspended = true;

}

synchronized void resume() {

suspended = false;

notify();

}

}

public class TestThread {

public static void main(String args[]) {

RunnableDemo R1 = new RunnableDemo( "Thread-1");

R1.start();

RunnableDemo R2 = new RunnableDemo( "Thread-2");

R2.start();

try {

Thread.sleep(1000);

R1.suspend();

System.out.println("Suspending First Thread");

Thread.sleep(1000);

R1.resume();

System.out.println("Resuming First Thread");

R2.suspend();

System.out.println("Suspending thread Two");

Thread.sleep(1000);

R2.resume();

System.out.println("Resuming thread Two");

} catch (InterruptedException e) {

System.out.println("Main thread Interrupted");

}try {

System.out.println("Waiting for threads to finish.");

R1.t.join();

R2.t.join();

} catch (InterruptedException e) {

System.out.println("Main thread Interrupted");

}

System.out.println("Main thread exiting.");

}

}

执行上面示例代码,得到以下结果 -

Creating Thread-1

Starting Thread-1

Creating Thread-2

Starting Thread-2

Running Thread-1

Thread: Thread-1, 10

Running Thread-2

Thread: Thread-2, 10

Thread: Thread-1, 9

Thread: Thread-2, 9

Thread: Thread-1, 8

Thread: Thread-2, 8

Thread: Thread-1, 7

Thread: Thread-2, 7

Suspending First Thread

Thread: Thread-2, 6

Thread: Thread-2, 5

Thread: Thread-2, 4

Resuming First Thread

Suspending thread Two

Thread: Thread-1, 6

Thread: Thread-1, 5

Thread: Thread-1, 4

Thread: Thread-1, 3

Resuming thread Two

Thread: Thread-2, 3

Waiting for threads to finish.

Thread: Thread-1, 2

Thread: Thread-2, 2

Thread: Thread-1, 1

Thread: Thread-2, 1

Thread Thread-1 exiting.

Thread Thread-2 exiting.

Main thread exiting.

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值