2 weixin 40098274 weixin_40098274 于 2017.09.08 20:21 提问

JAVA 如何暂停一个正在运行的线程,再启动另一个已经暂停的线程。

package MahJong;

import java.awt.Image;
import java.util.Iterator;

import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Card {
int i;
String img;
Card(int i,String img){
this.i = i;
this.img = img;
}
}
public class Frame extends JFrame{
Frame(){
new JFrame("MahJong Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(880, 660);
setLocation( 0, 0);
setVisible(true);
setResizable(false);
Container container = this.getContentPane();
container.setLayout(null);
ImageIcon background =new ImageIcon("MahJong.jpg");
background = new ImageIcon(background.getImage().getScaledInstance(880, 660, Image.SCALE_SMOOTH));
JLabel label =new JLabel(background);
label.setBounds(0, 0, this.getWidth(), this.getHeight());
JPanel mainPanel = (JPanel)this.getContentPane();
mainPanel.setOpaque(false);
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
repaint();
setVisible(true);
}
}
public class Button {
Card card ;
JButton btn = new JButton();
Button(Frame frame,Card card,int x,int y){
this.card = card;
String ima = this.card.img;
System.out.println(ima);
ImageIcon img = new ImageIcon(ima);
img = new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
btn.setLocation(x, y);
btn.setSize(45, 50);
btn.setIcon(img);
frame.add(btn);
frame.repaint();
}
Button(Frame frame,int x,int y){
btn.setLocation(x, y);
btn.setSize(45, 50);
btn.setContentAreaFilled(false);
frame.add(btn);
frame.repaint();
}
}
public class tools {
static int time=0,TIME = 0;
static void rank(Button[] btn,Frame Frame){
int i,j;Card card = null;
for(i=0;i for(j=0;j if(btn[j].card.i>btn[j+1].card.i){
card = btn[j].card;
btn[j].card=btn[j+1].card;
btn[j+1].card=card;
} } }

    for(i=0;i<13;i++){
        String ima = btn[i].card.img;
        ImageIcon img = new ImageIcon(ima);
        img = new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
        btn[i].btn.setIcon(img);}}
static void deal(Iterator ite,Button btn){
    Card card = (Card)ite.next();
    btn.card = card;
    String ima=card.img;
    ImageIcon img = new ImageIcon(ima);
    img = new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
    btn.btn.setIcon(img);
}
static void out(Button[] btn,Button Jbtn,Frame Frame,Button BTN,Iterator ite){
    Card out;
    out = Jbtn.card;
    TIME = time/17+TIME;
    if(time%17==0){time = 0;}
    JButton lose = new JButton();
    lose.setSize(45, 50);
    lose.setLocation((50+(time)*45), 135+TIME*55);
    String ima = out.img;
    ImageIcon img = new ImageIcon(ima);
    img =new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
    lose.setIcon(img);
    Frame.add(lose);
    Jbtn.card = BTN.card;
    ima = Jbtn.card.img;
    img = new ImageIcon(ima);
    img =new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
    Jbtn.btn.setIcon(img);
    BTN.card = null;
    BTN.btn.setIcon(null);
    BTN.btn.setContentAreaFilled(false);
    rank(btn,Frame);
    tools.deal(ite, BTN);
    Frame.repaint();
    time++;
}
static void out2(Button BTN,Frame frame,Iterator ite){
    Card out;
    out = BTN.card;
    TIME = time/17+TIME;
    if(time%17==0){time = 0;}
    JButton lose = new JButton();
    lose.setSize(45, 50);
    lose.setLocation((50+(time)*45), 135+TIME*55);
    String ima = out.img;
    ImageIcon img = new ImageIcon(ima);
    img =new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
    lose.setIcon(img);
    frame.add(lose);
    BTN.card = null;
    BTN.btn.setIcon(null);
    BTN.btn.setContentAreaFilled(false);
    tools.deal(ite, BTN);
    frame.repaint();
    time++;
}

}
public class Mythread extends Thread{
Frame frame;
Iterator ite;
public Mythread(String Name,Frame frame,Iterator ite){
setName(Name);
this.frame = frame;
this.ite =ite;
}
public void run(){
int x,y;
y =500;
Button[] btn = new Button[13];
for(int i=0;i<13;i++){
x =50+i*45;btn[i] = new Button(frame,(Card)ite.next(),x,y);
}tools.rank(btn,frame);
Button BTN = new Button(frame,660,y);
tools.deal(ite, BTN);
for(int i=0;i<13;i++){
Button jbtn = btn[i];
btn[i].btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tools.out(btn,jbtn,frame,BTN,ite);
}
});
}
BTN.btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tools.out2(BTN, frame,ite);
}
});
this.suspend();
}
}
public class AIthread extends Thread{
Frame frame;
Iterator ite;
public AIthread(String Name,Frame frame,Iterator ite){
setName(Name);
this.frame = frame;
this.ite =ite;
}
public void run(){
int x,y;
y =80;
Button[] btn = new Button[13];
for(int i=0;i<13;i++){
x =50+i*45;btn[i] = new Button(frame,(Card)ite.next(),x,y);
}tools.rank(btn,frame);
Button BTN = new Button(frame,660,y);
tools.deal(ite, BTN);
for(int i=0;i<13;i++){
Button jbtn = btn[i];
btn[i].btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tools.out(btn,jbtn,frame,BTN,ite);
}
});
}
BTN.btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tools.out2(BTN, frame,ite);
}
});

}

}
public class test {
public static void main(String[] agrs){
int i,j;
Frame mainFrame = new Frame();
ArrayList list = new ArrayList();
Card[] MJ = new Card[64];
String IMG[]={"A.jpg","B.jpg","C.jpg","D.jpg","E.jpg","F.jpg","G.jpg","H.jpg"
,"I.jpg","J.jpg","K.jpg","L.jpg","M.jpg","N.jpg","O.jpg","P.jpg"};
for(i=0;i<16;i++){
for(j=0;j<4;j++){
MJ[j+i*4] = new Card(i,IMG[i]);
list.add(MJ[j+i*4]);} }
Collections.shuffle(list);//打乱list
Iterator ite = list.iterator();
Mythread player = new Mythread("player",mainFrame,ite);
AIthread AI = new AIthread("AI",mainFrame,ite);
player.start();
AI.start();
}
}

3个回答

lianjunzongsiling
lianjunzongsiling   2017.09.08 20:59

你代码乱掉了


大致看了下,你应该是写个swing小程序
你想暂停一个线程,然后重启一个已经暂停的线程
建议你百度 java wait notifyAll
估计有你想要的东西

weixin_39863229
weixin_39863229   2017.09.08 21:24

看你是哪种暂停,如果让另一个线程暂停等待当前线程运行完毕就可以用join();
如果是同步情况下,用wait()和notify();

ljheee
ljheee   Rxr 2017.09.08 23:04

启动另一个已经暂停的线程,只能重新创建新的

Csdn user default icon
上传中...
上传图片
插入图片