#Multithreading: calling methods of an object in a thread, in a List of threads...

1 messages · Page 1 of 1 (latest)

peak sedge
#

So what I'm trying to do is the following:
Each time I call a method to "create a new Thread", a new Thread with parsed object is added to a List of type <Thread> (see code below).

public class NewThreads {
static ArrayList<Thread> threadList = new ArrayList<>():
static int i = 0;

public static void newThread(String name){
  threadList.add(new Thread(new myObject(name)));
  threadList.get(inst).start();
  inst ++;
}
}```
The myObject class implements Runnable.
In another class, I access the threads objects name variable as follows ("0" should simply access the first index of the list for testing):
```java
. . .
  System.out.print(NewThreads.threadList.get(0).getName();
. . .```
However, I can't call "getName()" on the thread (getName is a method of the myObject class). I don't know how to acces the object in the thread once it's in the list. I would appreciate any help.
mighty vineBOT
#

<@&987246399047479336> please have a look, thanks.

mighty vineBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

muted matrix
peak sedge
grim bluff
muted matrix
#

Something worth googling then :). It lets you submit a Callable, which is like a Runnable but it has a return value (unlike Runnable which has void return type).

After executor::submit, you get a Future, which at first will contain no value but once your Callable finishes it's job and returns something the value will appear in it. That is the value will eventually become available in the future. You can call Future::get which will block you program until Future's value becomes available.

peak sedge
# muted matrix Something worth googling then :). It lets you submit a `Callable`, which is like...

Padron me, but that didn't exactly say click inside my brain...
Just to clarify (because we're speaking about a return value now..). The threads I want to create contain an endless while(true) loop (until the program is terminated). I do not seek for them to return anything because that would mean their methods had ended. Instead I want them to loop ongoingly in the background of my program, while still allowing me to access their objects variables like I tried using my get method in the sample code.

muted matrix
# peak sedge Padron me, but that didn't exactly say click inside my brain... Just to clarify ...

Well, you can just access them by doing:

MyObject obj = new MyObject();
executors.submit(obj);
obj.getName();

But be careful. If you set a value in one thread and read it in another then you need use thread safety measures:

class MyObject implements Runnable {

  private volatile String name;

  public String getName() {
    return this.name;
  }

  @Override
  public void run() {
    this.name = "foo";
  }
}

Notice I made name volatile. It's difficult to really teach how to write thread safe code because you really need to study it yourself. I'd recommend the book Java Concurrency in Practice by Brian Goetz.

peak sedge
muted matrix
#

You're welcome

peak sedge
# muted matrix Well, you can just access them by doing: ```java MyObject obj = new MyObject();...

So as I'm reading into using the ExecutorService, and a question arises:
I have the idea to have a list of object instances (I will have multiple instances of the same object with different parameters/variables) and to "submit" each instance of this object to my fixed thread pool right after it's created in the corresponding method. What I can't seem to figure out is how to distinguish between the different instances in the different threads later on? As you said regarding thread safety, I'm setting different values to the same variable across different threads. How can I access e.g. t1, t2, etc.

grim bluff
#

those threads don't matter

#

though I wouldn't advise to executors actually

#

since you have infinite loops

#

you should use regular threads

peak sedge
peak sedge
grim bluff
#

just keep a reference to the object so you can call methods on it

peak sedge
# grim bluff wdym

I would do myObjectList.get(i).getName(); without mentioning the thread and it would still get me the current / up-to-date value from the thread it's in ?

grim bluff
# peak sedge Ok, so use executors only with finite methods?

executors are good when you simply want to execute tasks and you don't handle the burden of handling thread management
for example you have 100 tasks, and instead of creating a thread each time, you just supply them to an executor which will handle the thread creation and supply the tasks for you
but here, since you want an infinite loop, it will just fill the place in an executor for no reason

grim bluff
#

that's no how thread works

#

a thread will simply execute the run method

#

that's all

#

there is not this object is owned by a certain thread

peak sedge
peak sedge
#

So it stays in the same memory location

#

hence reference doesn't change

grim bluff
#

if you give a single object to 4 threads, then all 4 thread will execute the run method on the same object... and usually you will have problems

peak sedge
grim bluff
grim bluff
#

and this is not a question of memory location

peak sedge
#

Ok, think I gotcha

grim bluff
#

they are just different objects

#

also

#

what are you trying to do ?

peak sedge
# grim bluff what are you trying to do ?

haha, ye, perhaps a good idea to mention that ^^ :

I want to create a program that checks the data on a specific website. Each thread should check a different type of data (that's a different story). The method of the object x to check data y on the website should run every z seconds (periodically check the site for new data), thus an infinite loop with time.sleep() to create an interval.

#

I hope that's kind of relatable...

grim bluff
peak sedge
# grim bluff why do you want to use several threads ?

because I have a user interface where the user starts check1 to check for x, check2 to check for y, and so on. User should be able to determine which checks he wants to keep running. My idea was to have a new thread for every type of check, because each one requires a different site address to find the data.

#

If the check method finds data matching, that's displayed to the user without interrupting the other checks.

grim bluff
#

how is that related to several threads 🤔

#

how long is a check ?

peak sedge
#

~40 seconds

grim bluff
#

wait what

#

how can a check takes so much time ?

peak sedge
grim bluff
#

how can it takes so much time ?

#

it's not normal that you have to wait 40s

peak sedge
# grim bluff how can it takes so much time ?

Well it (Selenium) starts a new Browser each time, opens the page, waits for some annoying java script to load (I have a time.sleep(15) in there to make sure it's done), then navigates the page, and then only does it iterate through the data. Since it's a dynamic web-page and not pure html, I have to do all that manually with browser automation.

grim bluff
#

web scrapping ?

peak sedge
grim bluff
#

I can't help you anymore with that

peak sedge
grim bluff
peak sedge
#

Learning a bit of threads isn't gonna hurt either

peak sedge
peak sedge