「线程等待java」线程等待和超时等待
本篇文章给大家谈谈线程等待java,以及线程等待和超时等待对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
在java中同时起多个线程,然后怎么让多个线程进入等待状态?
class MyThread extends Thread {
static Object lock = new Object();
public void run() {
// TODO Auto-generated method stub
}
public void toWait() {
synchronized(lock){
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void toNotifyAll(){
synchronized(lock){
lock.notifyAll();
}
}
}
//让线程对象在run方法中调用toWait使其等待,调用静态方法toNotifyAll唤醒所有线程
java多线程相互等待
1、存在处以业务逻辑类,存在map,初始化。
2、存在线程池 最大200,ThreadFactory可以默认
3、单线程处理ip的类,构造方法包含是需参数Map,
4、外部循环 调用线程池,生成线程 传参 map 和当前循环次数
5、线程处理完ip后,将外部传来的循环次数做key,结果做value插入map,唤醒主线程判断map中的数量是否==需处理的ip的数量,选择跳出或继续等待。
用JAVA实现线程等待提示框[3]
② TestThread类
TestThread类是处理事务线程 即在标准输出设备上显示从 到
public class TestThread extends Thread {public void run(){ for (int i = ; i ; i++ ) {System out println(i); }} }
③ ThreadDiag类
ThreadDiag类用来显示 线程正在运行 提示框
import java awt *;import javax swing *;public class ThreadDiag extends Thread{ private Thread currentThread = null;//实际调用时就是TestThread事务处理线程 private String messages = ;//提示框的提示信息 private JFrame parentFrame = null;//提示框的父窗体 private JDialog clueDiag = null;// 线程正在运行 提示框 private Dimension dimensions = Toolkit getDefaultToolkit() getScreenSize(); private int width = dimensions width / height = ; private int left = top = ; public ThreadDiag(JFrame parentFrame Thread currentThread String messages) {this parentFrame = parentFrame;this currentThread = currentThread;this messages = messages;initDiag();//初始化提示框 } protected void initDiag(){clueDiag = new JDialog(parentFrame 正在执行 请等待 true);clueDiag setCursor(new Cursor(Cursor WAIT_CURSOR));JPanel testPanel = new JPanel();JLabel testLabel = new JLabel(messages);clueDiag getContentPane() add(testPanel);testPanel add(testLabel);(new DisposeDiag()) start();//启动关闭提示框线程 }public void run() {//显示提示框int left = (dimensions width width)/ ;int top = (dimensions height height)/ ;clueDiag setSize(new Dimension(width height));clueDiag setLocation(left top);clueDiag show(); }}
lishixinzhi/Article/program/Java/gj/201311/27677
Java如何等待子线程执行结束
先调用
shutdown
在调用
isTerminated
例:
/*
* 采用线程池开启多个子线程,主线程等待所有的子线程执行完毕
*/
public static void moreThread() {
try {
int threadNum = 0;
for (int i = 0; i 10; i++) {
threadNum++;
final int currentThreadNum = threadNum;
exe.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println("子线程[" + currentThreadNum + "]开启");
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("子线程[" + currentThreadNum + "]结束");
}
}
});
}
System.out.println("已经开启所有的子线程");
exe.shutdown();
System.out.println("shutdown():启动一次顺序关闭,执行以前提交的任务,但不接受新任务。");
while(true){
if(exe.isTerminated()){
System.out.println("所有的子线程都结束了!");
break;
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("主线程结束");
}
}
线程等待java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于线程等待和超时等待、线程等待java的信息别忘了在本站进行查找喔。