-1

I have a class which has a runnable thread in it. I send a variable to this class more than one time at the same time. The class has run() method and starts own thread for the variable sent from another class. After some processes, i need to stop Threads with interrupt but i have to spesify the name of the thread that i want to stop (such as MyThread.interrupt() ). The problem is, i create the thread with the same object name in the class everytime. So i have to name this thread object differently every time and stop it by its name. But i dont know how to.. This is my code;

public class KasaThread  {

public static Thread till ;

public static String [] table_list_kasa; 


public  static void get (final String IP,final String MagID){


     till = new Thread () {



            public void run () {


                if(IP != null){


                try{
                    ...
                    ...
                    ...
                    }catch(Exception e} {}

in another class, I interrupt this thread by its name like (till.interrupt() ).. But as i said, every thread which is created in this class, has the same name (till).

How to solve this ?

Thanks in advance.

Yasin Bilir
  • 194
  • 3
  • 17

3 Answers3

1

I think you can solve this problem with methods setName(String name) and getName() + some counter.

Sergey Frolov
  • 1,317
  • 1
  • 16
  • 30
  • there is no `getName` that takes a `String` in the `Thread` class. Also I don't see how this solves the problem. Yes, it names the threads, but provides no way to access them. – Oebele May 31 '16 at 07:04
  • well, yes, I mean this: `for(Thread thread: threads){ if (thread.getName().equals("yourName")) thread.interrupt();}` Or just to create a map in other class and controll your thread from there – Sergey Frolov May 31 '16 at 07:15
  • yes im doing this way. I created a map, I change the name of the thread by adding a number to the end (like till1, till2) and put the name of thread to the Map. But how do i call it from there? I need to use till2.interrupt(), but i have till2 varıable as String, not Thread. – Yasin Bilir May 31 '16 at 07:51
  • cant reach the thread that i named. Thread.currentThread() method returns threads include main, and cant stop that. I cant get thread variable like till2.interrupt(), i have it as String. what am i doing wrong ? – Yasin Bilir May 31 '16 at 08:21
  • 1
    Ok, i did it by changing the object name after creating, and then using it with Thread.currentThread(), i realised that this method returns the spesific thread that running at the time (not the main thread). so i can interrupt the thread like Thread.currentThread().interrupt(). Thanks for all your helps !! – Yasin Bilir May 31 '16 at 13:20
1

A solution to this would be holding a HashMap (or any other Id you like for the threads) in the other class, and make this one implement Runnable, so you can control your threads in the other class.

A. Valero
  • 31
  • 2
  • yes im doing this way. I created a map, I change the name of the thread by adding a number to the end (like till1, till2) and put the name of thread to the Map. But how do i call it from there? I need to use till2.interrupt(), but i have till2 varıable as String, not Thread. – Yasin Bilir May 31 '16 at 07:56
  • Yould do somethin like this `myMap.get(VAR_NAME + "1").interrupt()`, being VAR_NAME "till" defined as a attribute in the class. if "till" is not necessary, you can change the map to integer and only reference it by that number – A. Valero May 31 '16 at 08:06
  • cant i create the thread with different name everytime instead of changing it after creating ? Like till1 = new Thread(); till2 = new Thread() ... cant i change the variable name while creating ? – Yasin Bilir May 31 '16 at 08:30
  • There's no way to that. For that ou can use a Collection class (an arrayList) and run through that list by position. – A. Valero May 31 '16 at 08:33
  • Ok, i did it by changing the object name after creating, and then using it with Thread.currentThread(), i realised that this method returns the spesific thread that running at the time (not the main thread). so i can interrupt the thread like Thread.currentThread().interrupt(). Thanks for all your helps !! – Yasin Bilir May 31 '16 at 13:20
0

You could put the threads in an ArrayList or LinkedList. If you actually want to be able to access them by name, you could use a Map<String, Thread> for that.

Do you want to stop all threads at the same time? If so, you could add a stop() method to the class for that. It would probably look like this:

public void stop() {
    for (Thread thread : threads) {
        thread.interrupt()
    }
}
Oebele
  • 581
  • 1
  • 4
  • 17