java runnable 线程_java – 如何访问正在运行的线程/ runnable?

我有一个线程正在运行,但从外面我不能绕过一个值来停止该线程.如何在Mytest()中发送false / true值或调用运行的线程公共方法?当我按下按钮1?

例如:thread.interrupt(); runnable.stop();或runnable.start();

// Main

public class Main extends JFrame

{

public static Runnable runnable;

public static Thread thread;

private JButton b1 = new JButton("Start/Stop");

public void init()

{

//Execute a job on the event-dispatching thread:

try {

javax.swing.SwingUtilities.invokeAndWait(new Runnable()

{

public void run()

{

createGUI();

}

});

} catch (Exception e) {

System.err.println("createGUI didn't successfully complete");

}

}

public void createGUI()

{

Container cp = getContentPane();

b1.addActionListener(new button1()); cp.add(b1);

runnable = new Mytest();

thread = new Thread(runnable);

thread.start();

}

}

// Button 1 - [problem to go inside a running thread]

public class button1 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

System.out.println("button pressed - need to access ");

//thread.interrupt(); runnable.stop(); //or runnable.start();

}

}

// Running - Thread

public class Mytest implements Runnable

{

public static boolean onoff = false;

public static boolean status = false;

public void run()

{

while(true)

{

if (onoff)

{

return;

} else {

if (status==false) System.out.println("running");

}

}

}

public static void stop() { status = true; onoff=true; }

public static void start() { status = false; onoff = false; }

}

跟进(校对):

Step 1:

/* Main - boot/startup */

public class Main extends JFrame

{

public static Mytest runnable; // wrong: public static Runnable runnable;

public static Thread thread;

private JButton b1 = new JButton("Start");

private JButton b2 = new JButton("Stop");

public void init()

{

// Execute a job on the event-dispatching thread:

// In case Freezed for heavy lifting

try {

javax.swing.SwingUtilities.invokeAndWait(new Runnable()

{

public void run()

{

createGUI();

}

});

} catch (Exception e) {

System.err.println("createGUI didn't successfully complete");

}

}

public void createGUI()

{

Container cp = getContentPane();

b1.addActionListener(new button1());

cp.add(b1);

runnable = new Mytest();

thread = new Thread(runnable);

try {

thread.sleep(100); // value is milliseconds

thread.start();

} catch (InterruptedException e) {

}

}

public static void main(String[] args)

{

run(new Main(), 500, 500);

}

public static void run(JFrame frame, int width, int height)

{ ...

frame.setVisible(true);

}

}

/* To start */

public class button1 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

runnable.start();

}

}

/* To stop */

public class button2 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

runnable.stop();

}

}

Step 2:

/* Thread deals */

public class Mytest implements Runnable

{

private static volatile boolean running = true;

public void run()

{

while(running)

{

// do stuff

}

}

public void start() { running = true; }

public void stop() { running = false;}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值