#threading

1 messages · Page 1 of 1 (latest)

fair lotus
#

dont understand threading

tough horizonBOT
#

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

pseudo nymph
fair lotus
#

so like when i do start

#

what exactlu happens

#

i create another thread which invokes start?

#

and the start invokes run

pseudo nymph
#

@fair lotus do you know lambdas ?

fair lotus
#

like run me through the cucle of a thread

#

no

pseudo nymph
#
public class MyRunnable implements Runnable {
    @Override
    public void run() {
        // code to be executed by the new thread
    }
}

// Later
Thread myThread = new Thread(new MyRunnable());
myThread.start();

#

@fair lotus basically

fair lotus
#

hello

#

but u know

#

i saw some implementation

#

of class implementing runnable interface

#

then that runnable making an instance

#

and then doing instance.start()

#

whats the differebce

pseudo nymph
#

are they extending Thread ?

fair lotus
#

nah like implementing runnable interface

#

the thread class implements runnable as well

pseudo nymph
fair lotus
#

 class RunnableDemo implements  Runnable{
    private Thread t;
    private String threadname;
    public RunnableDemo(String name){
        threadname = name;

    }
    public void run(){
        System.out.println( threadname + " starting");
        try {
            Thread.sleep(50);
            for(int i = 0 ; i <100 ; i++){
                System.out.println(i);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
    public void start(){
        if(t == null){
            t = new Thread(this);
            t.start();
        }
    }
}
tough horizonBOT
# fair lotus ``` class RunnableDemo implements Runnable{ private Thread t; private...

Detected code, here are some useful tools:

Formatted code
class RunnableDemo implements Runnable {
  private Thread t;
  private String threadname;
  public RunnableDemo(String name) {
    threadname = name;
  }
  public void run() {
    System.out.println(threadname + " starting");
    try {
      Thread.sleep(50);
      for (int i = 0; i < 100; i++) {
        System.out.println(i);
      }
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
  }
  public void start() {
    if (t == null ) {
      t = new Thread(this );
      t.start();
    }
  }
}
fair lotus
#

the class

#

then the main

#
public class Main {
    public static void main(String[] args) {
        RunnableDemo r1 = new RunnableDemo("thread 1");
        r1.start();
        RunnableDemo r2 = new RunnableDemo("thread 2");
        r2.start();
    }
}```
tough horizonBOT
fair lotus
#

@pseudo nymph

proud karma
#

@fair lotus don't do that

#
    public void start(){
        if(t == null){
            t = new Thread(this);
            t.start();
        }
    }
#

this crap

fair lotus
#

explain

proud karma
#

you shouldn't have the code that defines the task also manage a thread

#

instead, just make a thread from the thing

#

that way you can also have the code work with executors

#

example

fair lotus
#

whats executors

#

anuwau

#

i wanted to ask

#

so the thread life cucle

#

is born thread

#

then it starts

#

then runs

#

then it either waits or terminates

hollow agate
#

you shouldnt create a Thread, most of the time having executors which handle all that is way better

fair lotus
#

damn

#

mu college from 3000 bc lol

hollow agate
#

well learning Thread is still important

fair lotus
#

ok

#

good

fair lotus
#

Starts the thread in a separate path of execution, then invokes the run() method on this Thread object.

#

this is an explanation

#

of run start method

#

what does separate path of execution mean

pseudo nymph
fair lotus
#

thats what i dont understand

#

so when we do like t1.start

#

t1 is an instance of thread

#

whu when we do t1.start();

#

it starts it in a separate thread

pseudo nymph
fair lotus
#

i just dont understand this

pseudo nymph
fair lotus
#

so inorder to invoke the start method it starts it in another thread

#

but doesnt that thread

#

also do the same

#

and then it an infinite loop

pseudo nymph
#

the start method starts a new thread

#

and then the run method is ran in this new thread

fair lotus
#

ok

fair lotus
#

btw i wanted to ask

#

if i did Thread t1 = new Thread();

#

then t1.start();

#

it would implemenbt the default run right?

proud karma
#

idk

#

try it

#

it will either run and finish immediately or crash

#

i'm not sure which