Manage Thread' s
When I press button [start] a new Thread will start , I want that Thread stop after pressing button [stop] ..
I tried to do it but ....
import java.awt.*;
import java.awt.event.*;
class Exp2{
publicstaticvoid main(String[] args){
new GUI();
}
}
class GUIextends Frame{
TextField tf;
Button start,stop;
GUI(){
setLayout(new FlowLayout());
tf =new TextField(20);
start =new Button("Start");
start.addActionListener(new Act(this));
stop =new Button("Stop");
stop.addActionListener(new Act(this));
add(tf);
add(start);
add(stop);
setSize(330,150);
setVisible(true);
}
}
class Actimplements ActionListener{
GUI target;
TCulc culc;
Act(GUI target){
this.target = target;
}
publicvoid actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("Start"))
begin();
if(cmd.equals("Stop"))
end();
}
publicvoid begin(){
culc =new TCulc(target);
culc.flag =true;
while(culc.flag){
try{
Thread.sleep(10);
}catch (InterruptedException ex){
}
}
}
publicvoid end(){
culc.flag =false;
}
}
class TCulcimplements Runnable{
Thread t;
Boolean flag =false;
GUI target;
TCulc(GUI target){
this.target = target;
t =new Thread(this);
t.start();
}
publicvoid run(){
while(flag){
for(int i = 0; i < 100; i++){
target.tf.setText(Integer.toString(i));
try{
Thread.currentThread().sleep(1000);
}catch (InterruptedException ex){
}
}
}
}
publicvoid setFlag(Boolean tf){
flag = tf;
}
}
[4597 byte] By [
Del2a] at [2007-11-26 13:48:38]

> When I press button [start] a new Thread will start ,
> I want that Thread stop after pressing button [stop]
In the early days of Java you could stop() a thread. The method is still
there (check the API documentation) but it's deprecated for a good
reason: slicing the throat of a thread while it can be in the middle of
a lot of acquired resources is dangerous and not safe. Nowadays
threads should be cooperative if you want them to be 'stoppable'.
IOW a thread should check whether or not another calling thread want
this thread to be stopped. Most of the time you have to implement a
'stoppable' Runnable (which can be a parameter to a Thread's ctor).
Something like this:public interface Stoppable extends Runnable {
public void stop();
}
...
public class MyStoppable implements Stoppable {
private boolean stop;
public void stop() { stop= true; }
public void run() {
while (!stop)
doSomethingThatDoesntTakeForever();
}
}
kind regards,
Jos
I tried do the same using boolean flag , but...Can You give full example (easy).
Del2a at 2007-7-8 1:24:45 >

You don't need an interface for that, thats just generating overhead. Either use a boolean or, perhaps even better, check the Thread itself. For example:
public class myThread implements Runnable {
private boolean threadControl;
public void stop() {
threadControl = false;
}
public void run() {
while (threadControl) {
// your code here
}
}
} // end of class
But you can even implement a check based on the thread itself. See http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html as mentioned in the API documentation.
interrupt the thread when you want to stop it, and catch the InterruptedException and return from run
public void run() {
while(flag) {
for(int i = 0; i < 100; i++){
target.tf.setText(Integer.toString(i));
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
return;
}
}
}
}
Look at my source I TRIED DO THE SAME
....
public void begin() {
culc = new TCulc(target);
culc.flag = true;
while(culc.flag){
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
}
}
}
public void end() {
culc.flag = false;
}
......
public void run() {
while(flag) {
for(int i = 0; i < 100; i++){
target.tf.setText(Integer.toString(i));
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
}
}
}
}
public void setFlag(Boolean tf){
flag = tf;
}
............
Please Help me realize this Idea Correctly
Message was edited by:
Del2
Del2a at 2007-7-8 1:24:45 >

> You don't need an interface for that, thats just generating overhead.Well, there's a little word of wisdom: "code to the interface, not to the implementation".kind regards,Jos
I notice you haven't yet explained what your problem is. Does the code not compile? Does it throw an exception? Does it not do what you think it should? Tell us which it is and provide some details.
And just looking at it, one problem with your code is that there is too much of it. I would just go through and remove pieces until it works... if I knew what your problem was.
> Well, there's a little word of wisdom: "code to the
> interface, not to the implementation".
Sure, but thats just what you're doing when implementing the Runnable interface. While it is a good practice its not the best approach per definition. For example, it might make changing your code at a later time a lot harder since you can't simply "change the interface and hope for the best".
>I notice you haven't yet explained what your problem is
>And just looking at it, one problem with your code is that there is too much of it.
Ok forgot about my code. Give me example that :
1)Have TextField , and two buttons [ start ] and [ stop ]
2)Button [ start ] run a new Thread that have method run like this :
public void run() {
//....
for(int i =0 ; i < 100; i++) {
// write i in TextField
}
}
3)Program (Thread which was run by button [ start ]) must stop in any time when I press button [ stop ]
My source code show that I tried realize idea with using boolean flag to say Thread that he must stop .
Del2a at 2007-7-8 1:24:45 >

Something weird about your code: your action listener has a loop in the method begin() (corresponding to the "start" button), a loop that looks a lot like that which is usually stuck in a run() method. As far as I can see, that will do nothing except keep the GUI event thread (I mean Java's GUI thread, nothing special with respect to your class "GUI") from running properly. Why did you do that?
It looks to me like the Act class is kind of unnecessary.It's far too heavy -- stuff like begin() and end() is really stuff that belongs in the class with the new running thread, IMHO. And like I said, the begin() method looks weird anyway.
I'd suggest removing Act and replacing it with a pair of anonymous inner classes...have the GUI class have its own reference to the TCulc class, and move begin() and end() to TCulc, only simplified.
Maybe something like this?
import java.awt.*;
import java.awt.event.*;
class Exp2 {
public static void main(String[] args){
new GUI();
}
}
class GUI extends Frame {
TextField tf;
Button start,stop;
TCulc tc;
GUI(){
setLayout(new FlowLayout());
tf = new TextField(20);
start = new Button("Start");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tc = new TCulc(this);
tc.begin();
}
});
stop = new Button("Stop");
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tc.end();
tc = null;
}
});
add(tf);
add(start);
add(stop);
setSize(330,150);
setVisible(true);
}
}
class TCulc implements Runnable {
private Thread t;
private boolean flag = false;
private GUI target;
TCulc(GUI target){
this.target = target;
}
public void run() {
while(flag) {
for(int i = 0; i < 100; i++){
target.tf.setText(Integer.toString(i));
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
}
}
}
}
public void begin() {
flag = true;
t = new Thread(this);
t.start();
}
public void end() {
flag = false;
}
}
Note that I haven't tried to compile or run that.
Your code not compile but before i change my code ..we get almost the same code .. my code is work better then before but still don't stop Thread ( now I can press button stop when Thread running )
import java.awt.*;
import java.awt.event.*;
class Exp2 {
public static void main(String[] args){
new GUI();
}
}
class GUI extends Frame {
TextField tf;
Button start,stop;
TCulc culc;
GUI(){
setLayout(new FlowLayout());
tf = new TextField(20);
start = new Button("Start");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
begin();
}
});
stop = new Button("Stop");
stop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
end();
}
});
add(tf);
add(start);
add(stop);
setSize(330,150);
setVisible(true);
}
public void begin(){
culc = new TCulc(this,true);
}
public void end(){
culc.setFlag(false);
}
}
class TCulc implements Runnable {
Thread t;
Boolean flag = false;
GUI target;
TCulc(GUI target,Boolean flag){
this.flag = flag;
this.target = target;
t = new Thread(this);
t.start();
}
public void run() {
while(flag) {
for(int i = 0; i < 100; i++){
target.tf.setText(Integer.toString(i));
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
}
}
}
}
public void setFlag(Boolean tf){
flag = tf;
}
}
Del2a at 2007-7-8 1:24:45 >

I Understand , i must insertif(!flag) break; in loop in my last post
public void run() {
while(flag) {
for(int i = 0; i < 100; i++){
System.out.println(flag);
if(!flag) break;
target.tf.setText(Integer.toString(i));
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
}
}
}
}
And now all work !
Somebody can remind me how add method for closing frame in awt like setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ?
Del2a at 2007-7-8 1:24:45 >
