Çalışan bir iş parçacığım var ancak dışarıdan bu iş parçacığını durdurmak için bir değeri atlayamıyorum. Mytest()
'un içinde yanlış/gerçek değeri nasıl gönderebilirim veya çalışan genel konuları mı çağırabilirim? Düğmeye1 bastığımda? örn: thread.interrupt();
runnable.stop();
veyaÇalışan konuya/çalıştırmaya nasıl erişilir?
// 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; }
}
runnable.start();
(okuma kanıtı) Takip:
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;}
}
, sadece tek döngü stoping edilir? Ama örnek hala kapalı değil mi? Bu kod modelinde mi? Sonra Thread.stop() veya Thread.interrupt() havuzu öldürmek için gerektirir? – YumYumYum
, "run" yöntemi sona erdiğinde thread durur. Gerekirse, Mytest örneğini yeni bir Thread'e ekleyebilir veya tekrar çalıştırmak istiyorsanız mevcut threadda 'start()' diyebilirsiniz. Bu kod sadece çalışan kodun makul bir zaman dilimi içinde tamamlanmasını sağlar (bayrak uçucu değilse durum böyle değildir) –
Teşekkürler! Lütfen yukarıdaki son güncellemeye bakın. Yani demek istediğimiz, bayrağı durdurmak için while döngüsünü işaretlersek, bu iş parçacığı otomatik olarak koparır ve eğer başlatabilirsek() iş parçacığı yaratır mı? – YumYumYum