Java 线程代码中 分开写了多个Thread.sleep(2000);却是在集中执行sleep效果如何制造成分散sleep的效果
importjava.awt.Color;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;im...
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;import javax.swing.JLabel;
public class MyGame {
class MyJfream extends JFrame{
JLabel []she=new JLabel[5];
{
for(int i=0;i<=she.length-1;i++){
she[i]=new JLabel();
she[i].setOpaque(true);
she[i].setBackground(Color.red);
this.add(she[i]);
she[i].setBounds(i/20*20+100, i%20*20+100, 20, 20);
}
she[0].setBackground(Color.BLUE);
she[1].setBackground(Color.cyan);
she[2].setBackground(Color.orange);
she[3].setBackground(Color.magenta);
she[4].setBackground(Color.pink);
}
public MyJfream() {
this.setBounds(500, 100, 600, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
this.getContentPane().setBackground(Color.gray);
this.setVisible(true);
jianting();
}
final int speed=20;
void jianting() {
KeyAdapter ka=new KeyAdapter() {
public void keyPressed(KeyEvent e) {
char ch=e.getKeyChar();
// 下面的代码分开写了5个sleep(2000)为什么实现的是sleep(10000);
// 如何造成分开的效果
if(ch=='s'){
she[0].setBounds(she[0].getX(),she[0].getY()+speed, 20, 20);
repaint();
try {
Thread.sleep(2000);
} catch (Exception e1) {
}
she[1].setBounds(she[1].getX(),she[1].getY()+speed, 20, 20);
repaint();
try {
Thread.sleep(2000);
} catch (Exception e1) {
}
she[2].setBounds(she[2].getX(),she[2].getY()+speed, 20, 20);
repaint();
try {
Thread.sleep(2000);
} catch (Exception e1) {
}
she[3].setBounds(she[3].getX(),she[3].getY()+speed, 20, 20);
repaint();
try {
Thread.sleep(2000);
} catch (Exception e1) {
}
she[4].setBounds(she[4].getX(),she[4].getY()+speed, 20, 20);
repaint();
try {
Thread.sleep(2000);
} catch (Exception e1) {
}
}
}
};
this.addKeyListener(ka);
}
}
public MyGame() {
new MyJfream();
}
public static void main(String[] args) {
new MyGame();
}
} 展开
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;import javax.swing.JLabel;
public class MyGame {
class MyJfream extends JFrame{
JLabel []she=new JLabel[5];
{
for(int i=0;i<=she.length-1;i++){
she[i]=new JLabel();
she[i].setOpaque(true);
she[i].setBackground(Color.red);
this.add(she[i]);
she[i].setBounds(i/20*20+100, i%20*20+100, 20, 20);
}
she[0].setBackground(Color.BLUE);
she[1].setBackground(Color.cyan);
she[2].setBackground(Color.orange);
she[3].setBackground(Color.magenta);
she[4].setBackground(Color.pink);
}
public MyJfream() {
this.setBounds(500, 100, 600, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
this.getContentPane().setBackground(Color.gray);
this.setVisible(true);
jianting();
}
final int speed=20;
void jianting() {
KeyAdapter ka=new KeyAdapter() {
public void keyPressed(KeyEvent e) {
char ch=e.getKeyChar();
// 下面的代码分开写了5个sleep(2000)为什么实现的是sleep(10000);
// 如何造成分开的效果
if(ch=='s'){
she[0].setBounds(she[0].getX(),she[0].getY()+speed, 20, 20);
repaint();
try {
Thread.sleep(2000);
} catch (Exception e1) {
}
she[1].setBounds(she[1].getX(),she[1].getY()+speed, 20, 20);
repaint();
try {
Thread.sleep(2000);
} catch (Exception e1) {
}
she[2].setBounds(she[2].getX(),she[2].getY()+speed, 20, 20);
repaint();
try {
Thread.sleep(2000);
} catch (Exception e1) {
}
she[3].setBounds(she[3].getX(),she[3].getY()+speed, 20, 20);
repaint();
try {
Thread.sleep(2000);
} catch (Exception e1) {
}
she[4].setBounds(she[4].getX(),she[4].getY()+speed, 20, 20);
repaint();
try {
Thread.sleep(2000);
} catch (Exception e1) {
}
}
}
};
this.addKeyListener(ka);
}
}
public MyGame() {
new MyJfream();
}
public static void main(String[] args) {
new MyGame();
}
} 展开
展开全部
这个好像是锁了吧,因为睡眠的时候不会释放锁,所以他睡眠的时候其他线程就只能等着时间到或者将他唤醒,记得有个生产者和消费者的问题就和你这个差不多,好像说是当生产者睡眠以后消费者就会在旁边一直等到他睡醒或者需要将他唤醒
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Thread.sleep需要放在synchronized关键字声明的代码块或者方法里 我好想从你代码里面找不到这个关键字
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你在代码块中或方法调用Thread.sleep需要的代码,我很怀念没有找到这个关键字synchronized关键字声明
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询