java thread 弃用_java-如何替换ThreadGroup中已弃用的方法this.s...

this.stop();

使用此方法的代码在::

ThreadedTestGroup.java ::

package utmj.threaded;

import junit.framework.*;

public class ThreadedTestGroup extends ThreadGroup {

private Test test;

private TestResult testResult;

public ThreadedTestGroup(Test test) {

super("ThreadedTestGroup");

this.test = test;

}

public void interruptThenStop() {

this.interrupt();

if (this.activeCount() > 0) {

this.stop(); // For those threads which won't interrupt

}

}

public void setTestResult(TestResult result) {

testResult = result;

}

public void uncaughtException(Thread t, Throwable e) {

if (e instanceof ThreadDeath) {

return;

}

if (e instanceof AssertionFailedError) {

testResult.addFailure(test, (AssertionFailedError) e);

} else {

testResult.addError(test, e);

}

this.interruptThenStop();

}

}

CobcyrrentTestCase.java

package utmj.threaded;

import java.util.*;

import junit.framework.*;

/

public class ConcurrentTestCase extends TestCase {

private TestResult currentResult;

private ThreadedTestGroup threadGroup;

private Hashtable threads = new Hashtable();

private boolean deadlockDetected = false;

pri

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java的线程是通过Thread类来实现的,Thread类封装了所有线程相关的方法和属性。下面是Thread类的部分源码: ```java public class Thread implements Runnable { //线程状态 private volatile int threadStatus = 0; private static final int RUNNING = 1; private static final int SHUTDOWN = -1; private static final int STOP = -2; private static final int TIDYING = 2; private static final int TERMINATED = 3; //线程优先级 public final static int MIN_PRIORITY = 1; public final static int NORM_PRIORITY = 5; public final static int MAX_PRIORITY = 10; //线程组 private ThreadGroup group; private Runnable target; private String name; private long stackSize; private long eetop; //线程ID private long tid; //线程本地存储 ThreadLocal.ThreadLocalMap threadLocals = null; ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; //线程断标志 private volatile boolean interrupted = false; private static final int HIBERNATE = 0; private static final int WAITING = 1; private static final int TIMED_WAITING = 2; private static final int BLOCKED = 3; private static final int NEW = 0; //线程锁 private Object parkBlocker; //线程断处理 private void handleInterrupt() { if (this != Thread.currentThread()) throw new RuntimeException("Only the original thread can be interrupted"); if (this.interrupted) { park(); } } //线程休眠 public static void sleep(long millis) throws InterruptedException { Thread.sleep(millis, 0); } //断线程 public void interrupt() { if (this != Thread.currentThread()) checkAccess(); synchronized (this) { interrupted = true; notifyAll(); } } //线程运行方法 @Override public void run() { if (target != null) { target.run(); } } } ``` 在Thread,我们可以看到一些重要的属性和方法,比如: - threadStatus: 线程状态,用整数表示,包含RUNNING、SHUTDOWN、STOP、TIDYING、TERMINATED五种状态。 - group: 线程所属的线程组。 - target: 线程要执行的任务。 - name: 线程名称。 - stackSize: 线程堆栈大小。 - tid: 线程ID。 - threadLocals: 线程本地存储。 - interrupted: 线程断标志。 - parkBlocker: 线程锁。 - sleep(): 线程休眠方法。 - interrupt(): 断线程方法。 - run(): 线程运行方法。 通过这些属性和方法,我们可以使用Java的线程实现多线程编程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值