導航:首頁 > 編程語言 > javastateflag

javastateflag

發布時間:2025-01-13 01:51:15

java Thread BLOCKED和WAITING兩種狀態的區別

BLOCKED狀態
線程處於BLOCKED狀態的場景。
當前線程在等待一個monitor lock,比如等待執行synchronized代碼塊或者使用synchronized標記的方法。
在synchronized塊中循環調用Object類型的wait方法,如下是樣例
synchronized(this)
{
while (flag)
{
obj.wait();
}
// some other code
}
WAITING狀態
線程處於WAITING狀態的場景。
調用Object對象的wait方法,但沒有指定超時值。
調用Thread對象的join方法,但沒有指定超時值。
調用LockSupport對象的park方法。
提到WAITING狀態,順便提一下TIMED_WAITING狀態的場景。
TIMED_WAITING狀態
線程處於TIMED_WAITING狀態的場景。
調用Thread.sleep方法。
調用Object對象的wait方法,指定超時值。
調用Thread對象的join方法,指定超時值。
調用LockSupport對象的parkNanos方法。
調用LockSupport對象的parkUntil方法。

㈡ 關於java中的interrupt()方法疑問

當一個線程處於不可運行狀態時,如鍵盤輸入,調用Thread.join()方法或者Thread.sleep()方法,從而線程被阻塞了。調用interrupt()可以使一個被阻塞的線程拋出一個中斷異常,從而使線程提前結束阻塞狀態,退出堵塞代碼。
你可參照下http://daydayup1989.javaeye.com/blog/785581

㈢ 一個畫直線的JAVA小程序

//你的類我改了錯誤,現在可以用了

//有錯的地方我標了「//有錯」

//請認真看,我花了時間改的
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class DrawLine {

public static void main(String[] args) {

new MyFrame(200, 200, 300, 300);
}
}

class MyFrame extends Frame {

Point p = new Point();
Point q = new Point();
boolean flag;
ArrayList<Point> a1 = new ArrayList<Point>();
ArrayList<Point> a2 = new ArrayList<Point>();

MyFrame(int x, int y, int w, int h) {
super("drawline");
setBounds(x, y, w, h);
setBackground(Color.white);
Button b = new Button("Line");
add(b, BorderLayout.NORTH);
b.addActionListener(new ButtonMonitor());
addMouseListener(new MouseMonitor());
addWindowListener(new WindowMonitor());
setVisible(true);
}

class ButtonMonitor implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand() == "Line") {
flag = true;
}
repaint();
}
}

public void paint(Graphics g) {

Iterator<Point> i1 = a1.iterator();
Iterator<Point> i2 = a2.iterator();

while (i1.hasNext() && i2.hasNext()) {
p = i1.next();
q = i2.next();//有錯
Color c = g.getColor();
g.setColor(Color.red);
g.drawLine(p.x, p.y, q.x, q.y);
g.setColor(c);
}

}

public void addPoint1(Point p) {
a1.add(p);
}

public void addPoint2(Point p) {
a2.add(p);
}

class MouseMonitor extends MouseAdapter {

public void mousePressed(MouseEvent e) {

MyFrame my = (MyFrame) e.getSource();
addPoint1(new Point(e.getX(), e.getY()));//有錯 my->e
repaint();
}

public void mouseReleased(MouseEvent e) {//有錯mouseDragged->mouseReleased

MyFrame my = (MyFrame) e.getSource();
addPoint2(new Point(e.getX(), e.getY()));//有錯
repaint();
}
}

class WindowMonitor extends WindowAdapter {

@Override
public void windowClosing(WindowEvent e) {

setVisible(false);
System.exit(0);
}
}
}

閱讀全文

與javastateflag相關的資料

熱點內容
js給php變數賦值 瀏覽:446
雜志版本號是什麼意思 瀏覽:223
地圖特效代碼 瀏覽:192
去除思科配置文件中的號 瀏覽:196
運行的16位程序太多 瀏覽:1
蘋果mac用什麼軟體好學編程 瀏覽:681
ai中線段怎麼添加寬度配置文件 瀏覽:956
lol文件怎麼找不到game 瀏覽:142
aecc視頻教程 瀏覽:983
linux怎麼查看資料庫用戶名 瀏覽:182
cefs文件系統 瀏覽:404
學平面設計個編程哪個好 瀏覽:701
如何把編程文件轉為hex 瀏覽:80
清除蘋果地圖來自地址 瀏覽:233
已經打開的文件如何清理 瀏覽:685
視頻網站有什麼用 瀏覽:70
多個表格文件怎樣壓縮文件 瀏覽:729
cad文件大很卡如何解決 瀏覽:633
將java程序打包成apk 瀏覽:277
2021唱吧文件找不到了 瀏覽:463

友情鏈接