只有一个核心,才会出做答现,优先级别高的先完成!
线程的优先级,尽管能更纯谨慧好的管理你的程序,但这不符合java编程的要去,java要求线程同级别,这样的java程序才是优秀的!
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela
//修改了,添加了start、stop方法、并且在run方法中添加判断。。。import java.awt.*
import javax.swing.*
public class test1 {
public static void main(String[] args) {
JFrame frm = new JFrame("Java Farme")
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
ball B1 = new ball(50, 50)
frm.add(B1)
B1.start()
frm.setBounds(400, 200, 700, 400)
frm.setVisible(true)
}
}
class ball extends JPanel implements Runnable {
int x
int y
Thread thread
public ball(int x, int y) {
this.x = x
返伍手 this.y = y
}
public void paint(Graphics g) {
super.paint(g)
g.setColor(Color.blue)
g.fillOval(x, y, 40, 40)
g.dispose()
}
public void start() {
if (null == thread) {
thread = new Thread(this)
thread.start()
}
漏嫌 }
public synchronized void stop() {
if (null != thread) {
thread.interrupt()
thread = null
notifyAll()
}
}
public void run() {
Thread me = Thread.currentThread()
for ( me == thread) {
if (x < 400) {
x += 50
this.repaint()
} else {
x = 0
this.repaint()
}
try {
橘肆 Thread.sleep(100)
} catch (InterruptedException ex) {}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)