How recreate thread with new parameters?












0














I have multithreading program, which send email to my clients.

In my code: thread have some troubles - code just close window of firefox and that's it.

I want to create these algorithm: thread have some troubles - window of firefox (+selenium) just close(only one thread is closing, another threads should continue working), get another parameters data from my DB and start thread again with new parameters

And all of time should working 5-10 threads non-stop. If something goes wrong with thread - firefox window should be closed, program get another data from DB(i'll create these code, it's easy) and should open new firefox window and continue working with new parameters.



Main method:



static String path = "C:\Users\Admin\Desktop\clients.txt";
static String path_ok = "C:\Users\Admin\Desktop\clients2.txt";
static Integer numberMail = 1; //id рассылки
static List<User> users = new ArrayList<User>();

YandexConfig config1 = new YandexConfig("login1", "password1", "cookies", port1);
YandexConfig config2 = new YandexConfig("login2", "password2", "cookies", port2);
ExecutorService executor = Executors.newFixedThreadPool(50);
CompletableFuture.runAsync(new DoThread("thread 1", path, path_ok, numberMail, users, config1), executor);
CompletableFuture.runAsync(new DoThread("thread 2", path, path_ok, numberMail, users, config2), executor);


doThread:



class DoThread implements Runnable {
private String threadName;
private Integer numberMail;
private String themeMessage = "Your order";
private String messageDefault = "blabla";
private final List<User> users;
private final String fileWait;
private final String fileOk;
private final YandexConfig config;

DoThread(String threadName, String fileWait, String fileOk, Integer numberMail, List<User> users, YandexConfig config) {
this.threadName = threadName;
this.fileWait = fileWait;
this.fileOk = fileOk;
this.numberMail = numberMail;
this.users = users;
this.config = config;
}

public void run() {
System.out.println("Thread #" + threadName + " started");

while (true) {
try {
auth(threadName, config.login, config.password);
break;
} catch (InterruptedException e) {

System.out.println("Something goes wrong");

e.printStackTrace();
}
}
//another a lot of strings of code
}
}


Question is: how I can restart thread with new parameters based on my code?










share|improve this question



























    0














    I have multithreading program, which send email to my clients.

    In my code: thread have some troubles - code just close window of firefox and that's it.

    I want to create these algorithm: thread have some troubles - window of firefox (+selenium) just close(only one thread is closing, another threads should continue working), get another parameters data from my DB and start thread again with new parameters

    And all of time should working 5-10 threads non-stop. If something goes wrong with thread - firefox window should be closed, program get another data from DB(i'll create these code, it's easy) and should open new firefox window and continue working with new parameters.



    Main method:



    static String path = "C:\Users\Admin\Desktop\clients.txt";
    static String path_ok = "C:\Users\Admin\Desktop\clients2.txt";
    static Integer numberMail = 1; //id рассылки
    static List<User> users = new ArrayList<User>();

    YandexConfig config1 = new YandexConfig("login1", "password1", "cookies", port1);
    YandexConfig config2 = new YandexConfig("login2", "password2", "cookies", port2);
    ExecutorService executor = Executors.newFixedThreadPool(50);
    CompletableFuture.runAsync(new DoThread("thread 1", path, path_ok, numberMail, users, config1), executor);
    CompletableFuture.runAsync(new DoThread("thread 2", path, path_ok, numberMail, users, config2), executor);


    doThread:



    class DoThread implements Runnable {
    private String threadName;
    private Integer numberMail;
    private String themeMessage = "Your order";
    private String messageDefault = "blabla";
    private final List<User> users;
    private final String fileWait;
    private final String fileOk;
    private final YandexConfig config;

    DoThread(String threadName, String fileWait, String fileOk, Integer numberMail, List<User> users, YandexConfig config) {
    this.threadName = threadName;
    this.fileWait = fileWait;
    this.fileOk = fileOk;
    this.numberMail = numberMail;
    this.users = users;
    this.config = config;
    }

    public void run() {
    System.out.println("Thread #" + threadName + " started");

    while (true) {
    try {
    auth(threadName, config.login, config.password);
    break;
    } catch (InterruptedException e) {

    System.out.println("Something goes wrong");

    e.printStackTrace();
    }
    }
    //another a lot of strings of code
    }
    }


    Question is: how I can restart thread with new parameters based on my code?










    share|improve this question

























      0












      0








      0







      I have multithreading program, which send email to my clients.

      In my code: thread have some troubles - code just close window of firefox and that's it.

      I want to create these algorithm: thread have some troubles - window of firefox (+selenium) just close(only one thread is closing, another threads should continue working), get another parameters data from my DB and start thread again with new parameters

      And all of time should working 5-10 threads non-stop. If something goes wrong with thread - firefox window should be closed, program get another data from DB(i'll create these code, it's easy) and should open new firefox window and continue working with new parameters.



      Main method:



      static String path = "C:\Users\Admin\Desktop\clients.txt";
      static String path_ok = "C:\Users\Admin\Desktop\clients2.txt";
      static Integer numberMail = 1; //id рассылки
      static List<User> users = new ArrayList<User>();

      YandexConfig config1 = new YandexConfig("login1", "password1", "cookies", port1);
      YandexConfig config2 = new YandexConfig("login2", "password2", "cookies", port2);
      ExecutorService executor = Executors.newFixedThreadPool(50);
      CompletableFuture.runAsync(new DoThread("thread 1", path, path_ok, numberMail, users, config1), executor);
      CompletableFuture.runAsync(new DoThread("thread 2", path, path_ok, numberMail, users, config2), executor);


      doThread:



      class DoThread implements Runnable {
      private String threadName;
      private Integer numberMail;
      private String themeMessage = "Your order";
      private String messageDefault = "blabla";
      private final List<User> users;
      private final String fileWait;
      private final String fileOk;
      private final YandexConfig config;

      DoThread(String threadName, String fileWait, String fileOk, Integer numberMail, List<User> users, YandexConfig config) {
      this.threadName = threadName;
      this.fileWait = fileWait;
      this.fileOk = fileOk;
      this.numberMail = numberMail;
      this.users = users;
      this.config = config;
      }

      public void run() {
      System.out.println("Thread #" + threadName + " started");

      while (true) {
      try {
      auth(threadName, config.login, config.password);
      break;
      } catch (InterruptedException e) {

      System.out.println("Something goes wrong");

      e.printStackTrace();
      }
      }
      //another a lot of strings of code
      }
      }


      Question is: how I can restart thread with new parameters based on my code?










      share|improve this question













      I have multithreading program, which send email to my clients.

      In my code: thread have some troubles - code just close window of firefox and that's it.

      I want to create these algorithm: thread have some troubles - window of firefox (+selenium) just close(only one thread is closing, another threads should continue working), get another parameters data from my DB and start thread again with new parameters

      And all of time should working 5-10 threads non-stop. If something goes wrong with thread - firefox window should be closed, program get another data from DB(i'll create these code, it's easy) and should open new firefox window and continue working with new parameters.



      Main method:



      static String path = "C:\Users\Admin\Desktop\clients.txt";
      static String path_ok = "C:\Users\Admin\Desktop\clients2.txt";
      static Integer numberMail = 1; //id рассылки
      static List<User> users = new ArrayList<User>();

      YandexConfig config1 = new YandexConfig("login1", "password1", "cookies", port1);
      YandexConfig config2 = new YandexConfig("login2", "password2", "cookies", port2);
      ExecutorService executor = Executors.newFixedThreadPool(50);
      CompletableFuture.runAsync(new DoThread("thread 1", path, path_ok, numberMail, users, config1), executor);
      CompletableFuture.runAsync(new DoThread("thread 2", path, path_ok, numberMail, users, config2), executor);


      doThread:



      class DoThread implements Runnable {
      private String threadName;
      private Integer numberMail;
      private String themeMessage = "Your order";
      private String messageDefault = "blabla";
      private final List<User> users;
      private final String fileWait;
      private final String fileOk;
      private final YandexConfig config;

      DoThread(String threadName, String fileWait, String fileOk, Integer numberMail, List<User> users, YandexConfig config) {
      this.threadName = threadName;
      this.fileWait = fileWait;
      this.fileOk = fileOk;
      this.numberMail = numberMail;
      this.users = users;
      this.config = config;
      }

      public void run() {
      System.out.println("Thread #" + threadName + " started");

      while (true) {
      try {
      auth(threadName, config.login, config.password);
      break;
      } catch (InterruptedException e) {

      System.out.println("Something goes wrong");

      e.printStackTrace();
      }
      }
      //another a lot of strings of code
      }
      }


      Question is: how I can restart thread with new parameters based on my code?







      java multithreading selenium






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 18:39









      via viavia via

      13




      13
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You need a way to modify the parameters for the next thread. You need to supply setters for the data and then just start up another thread.



          class DoThread implements Runnable {
          ...
          void setThreadName(String name) { ... }
          void setFileOK(String fileOK) { ... }
          }

          class UseDoThread {
          void f() {
          DoThread r = new DoThread(...);
          new Thread(r).start();
          ...
          r.setThreadName("?");
          r.setFileOK("I think so");
          new Thread(r).start();
          }


          However, warning: You would need to wait for the thread to finish before modifying the values and restarting. Otherwise, the first run will use the modified values and your data will be essentially corrupt. You can wait for the thread to finish in a number of wait, most simply by using the join method on it.



          But if you can simply join it, then you might not be benefitting from making this multithreaded in the first place.



          A better approach might be to make a new instance of DoThread and start that.



          void f() {
          new Thread(new DoThread("thread1", ...)).start();
          new Thread(new DoThread("thread2", ...)).start();
          }


          It is for this type of reason that throwaway classes with final values are used more in concurrent programming. Each thread needs its own variant, and if they are all going at once it does not make sense to try to recycle this type of class. So you could declare all of the fields in DoThread to be final if you plan to create a new DoThread each time.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380788%2fhow-recreate-thread-with-new-parameters%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You need a way to modify the parameters for the next thread. You need to supply setters for the data and then just start up another thread.



            class DoThread implements Runnable {
            ...
            void setThreadName(String name) { ... }
            void setFileOK(String fileOK) { ... }
            }

            class UseDoThread {
            void f() {
            DoThread r = new DoThread(...);
            new Thread(r).start();
            ...
            r.setThreadName("?");
            r.setFileOK("I think so");
            new Thread(r).start();
            }


            However, warning: You would need to wait for the thread to finish before modifying the values and restarting. Otherwise, the first run will use the modified values and your data will be essentially corrupt. You can wait for the thread to finish in a number of wait, most simply by using the join method on it.



            But if you can simply join it, then you might not be benefitting from making this multithreaded in the first place.



            A better approach might be to make a new instance of DoThread and start that.



            void f() {
            new Thread(new DoThread("thread1", ...)).start();
            new Thread(new DoThread("thread2", ...)).start();
            }


            It is for this type of reason that throwaway classes with final values are used more in concurrent programming. Each thread needs its own variant, and if they are all going at once it does not make sense to try to recycle this type of class. So you could declare all of the fields in DoThread to be final if you plan to create a new DoThread each time.






            share|improve this answer


























              0














              You need a way to modify the parameters for the next thread. You need to supply setters for the data and then just start up another thread.



              class DoThread implements Runnable {
              ...
              void setThreadName(String name) { ... }
              void setFileOK(String fileOK) { ... }
              }

              class UseDoThread {
              void f() {
              DoThread r = new DoThread(...);
              new Thread(r).start();
              ...
              r.setThreadName("?");
              r.setFileOK("I think so");
              new Thread(r).start();
              }


              However, warning: You would need to wait for the thread to finish before modifying the values and restarting. Otherwise, the first run will use the modified values and your data will be essentially corrupt. You can wait for the thread to finish in a number of wait, most simply by using the join method on it.



              But if you can simply join it, then you might not be benefitting from making this multithreaded in the first place.



              A better approach might be to make a new instance of DoThread and start that.



              void f() {
              new Thread(new DoThread("thread1", ...)).start();
              new Thread(new DoThread("thread2", ...)).start();
              }


              It is for this type of reason that throwaway classes with final values are used more in concurrent programming. Each thread needs its own variant, and if they are all going at once it does not make sense to try to recycle this type of class. So you could declare all of the fields in DoThread to be final if you plan to create a new DoThread each time.






              share|improve this answer
























                0












                0








                0






                You need a way to modify the parameters for the next thread. You need to supply setters for the data and then just start up another thread.



                class DoThread implements Runnable {
                ...
                void setThreadName(String name) { ... }
                void setFileOK(String fileOK) { ... }
                }

                class UseDoThread {
                void f() {
                DoThread r = new DoThread(...);
                new Thread(r).start();
                ...
                r.setThreadName("?");
                r.setFileOK("I think so");
                new Thread(r).start();
                }


                However, warning: You would need to wait for the thread to finish before modifying the values and restarting. Otherwise, the first run will use the modified values and your data will be essentially corrupt. You can wait for the thread to finish in a number of wait, most simply by using the join method on it.



                But if you can simply join it, then you might not be benefitting from making this multithreaded in the first place.



                A better approach might be to make a new instance of DoThread and start that.



                void f() {
                new Thread(new DoThread("thread1", ...)).start();
                new Thread(new DoThread("thread2", ...)).start();
                }


                It is for this type of reason that throwaway classes with final values are used more in concurrent programming. Each thread needs its own variant, and if they are all going at once it does not make sense to try to recycle this type of class. So you could declare all of the fields in DoThread to be final if you plan to create a new DoThread each time.






                share|improve this answer












                You need a way to modify the parameters for the next thread. You need to supply setters for the data and then just start up another thread.



                class DoThread implements Runnable {
                ...
                void setThreadName(String name) { ... }
                void setFileOK(String fileOK) { ... }
                }

                class UseDoThread {
                void f() {
                DoThread r = new DoThread(...);
                new Thread(r).start();
                ...
                r.setThreadName("?");
                r.setFileOK("I think so");
                new Thread(r).start();
                }


                However, warning: You would need to wait for the thread to finish before modifying the values and restarting. Otherwise, the first run will use the modified values and your data will be essentially corrupt. You can wait for the thread to finish in a number of wait, most simply by using the join method on it.



                But if you can simply join it, then you might not be benefitting from making this multithreaded in the first place.



                A better approach might be to make a new instance of DoThread and start that.



                void f() {
                new Thread(new DoThread("thread1", ...)).start();
                new Thread(new DoThread("thread2", ...)).start();
                }


                It is for this type of reason that throwaway classes with final values are used more in concurrent programming. Each thread needs its own variant, and if they are all going at once it does not make sense to try to recycle this type of class. So you could declare all of the fields in DoThread to be final if you plan to create a new DoThread each time.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 18:50









                AaronAaron

                857414




                857414






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380788%2fhow-recreate-thread-with-new-parameters%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    Npm cannot find a required file even through it is in the searched directory