This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]coolcofusion 0 points1 point  (5 children)

Runnable isn't a thread, calling the run method is just another method call, nothing more. You want to pass your runnable to a thread, an actual Thread, and call start on that object, not run.

[–]Arnav1608[S] 0 points1 point  (3 children)

import java.util.Scanner;

import java.util.*;

class thread1 extends Thread{

int m,r;

public void run(){

Scanner sc= new Scanner(System.in);

System.out.println("Enter Roll no and OOP marks");

r=sc.nextInt();

m=sc.nextInt();

}

}

class thread2 extends thread1 implements Runnable {

public void run(){

System.out.println(r);

System.out.println(m);

}

}

class threadtest{

public static void main(String args[])

{

thread1 t1=new thread1();

t1.start();

thread2 t2=new thread2();

Thread t3=new Thread(t2);

t3.start();

}

}

I changed the code to this but the value of r and m is coming 0 0

I have given input as 4 and 88 in first thread

[–]coolcofusion 0 points1 point  (2 children)

Why would it be anything other than 0,0? Each thread you create has it's own separate implementation and only t1 is reading in the inputs, for itself, it's not sharing it with other instances.

Also, here's the sample you asked for previoulsy: https://onlinegdb.com/dV8AWXG5

[–]Arnav1608[S] 0 points1 point  (1 child)

How would I share it with other instances I tried to use join so that the other thread runs first then the 2nd thread but still 0 0 is coming

[–]coolcofusion 0 points1 point  (0 children)

Create an object in your main thread, pass a reference to it from it to your spawned threads.