#threading
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
What do you not understand ?
so like when i do start
what exactlu happens
i create another thread which invokes start?
and the start invokes run
@fair lotus do you know lambdas ?
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
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
are they extending Thread ?
nah like implementing runnable interface
the thread class implements runnable as well
show me this 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();
}
}
}
Detected code, here are some useful tools:
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();
}
}
}
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();
}
}```
Detected code, here are some useful tools:
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();
}
}
@pseudo nymph
@fair lotus don't do that
public void start(){
if(t == null){
t = new Thread(this);
t.start();
}
}
this crap
explain
A browser interface to the Java compiler to experiment with the language
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
A browser interface to the Java compiler to experiment with the language
example
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
the modern way of using concurrency
you shouldnt create a Thread, most of the time having executors which handle all that is way better
well learning Thread is still important
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
Probably mean in another thread
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
what's the problem?
i just dont understand this
what do you not understand?
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
no
the start method starts a new thread
and then the run method is ran in this new thread
ok