Why am I getting NullPointerException here
package Threads;
public class Test implements Runnable {
FF d;
public static void main(String[] args) {
new Test().go();
}
void go() {
System.out.println(Thread.currentThread());
d = new FF();
new Thread(new Test()).start();
new Thread(new Test()).start();
}
public void run() {
System.out.println(d);
System.out.println(Thread.currentThread());
d.chat(Thread.currentThread().getId());//RTE
}
}
class FF {
static long flag = 0;
void chat(long id) {
if(flag == 0) {
flag = id;
}
for(int x=1;x<3;x++) {
if(flag == id) {
System.out.println("Yo");
}else {
System.out.println("dude");
}
}
}
}
//can anybody explain why I am getting NullPointerException here.I though d reference variable was intialized in go() method but I think it has something to do when new Thread is created.
d is an instance field for class Test. Initializing it for one instance of Test does not make it initialized for all instances that are subsequently created. You have initialized this field in the go method of the instance you create in main. But after that, you create new instances of Test for which their field is not initialized yet:
new Thread(new Test()).start(); // d is not initialized for the new Test() object
new Thread(new Test()).start();
One solution is to initialize the field in the constructor:
public Test() {
d = new FF();
}
you're creating a new Test instance and thus d is not initialized (since the thread.start method doesn't call your go method but only the run method).
Related
This is a pseudocode version of my current working code:
public class DataTransformer {
private final boolean async = true;
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
public void modifyAsync(Data data) {
if (async) {
executorService.submit(new Runnable() {
#Override
public void run() {
modify(data);
}
});
} else {
modify(data);
}
}
// This should actually be a variable inside modify(byte[] data)
// But I reuse it to avoid reallocation
// This is no problem in this case
// Because whether or not async is true, only one thread is used
private final byte[] temp = new byte[1024];
private void modify(Data data) {
// Do work using temp
data.setReady(true); // Sets a volatile flag
}
}
Please read the comments. But now I want to use Executors.newFixedThreadPool(10) instead of Executors.newSingleThreadExecutor(). This is easily possible in my case by moving the field temp inside modify(Data data), such that each execution has it's own temp array. But that's not what I want to do because i want to reuse the array if possible. Instead I want for each of the 10 threads a temp array. What's the best way to achieve this?
As static variable is shared between all Threads, so you could declare as static. But if you want to use different values then either use Threadlocal or use different object.
With ThreadLocal you could do :
ThreadLocal<byte[]> value = ThreadLocal.withInitial(() -> new byte[1024]);
You could also use object like this:
public class Test {
public static void main(String[] args) {
try {
Test test = new Test();
test.test();
} catch (Exception e) {
e.printStackTrace();
}
}
class Control {
public volatile byte[] temp = "Hello World".getBytes();
}
final Control control = new Control();
class T1 implements Runnable {
#Override
public void run() {
String a = Arrays.toString(control.temp);
System.out.println(a);
}
}
class T2 implements Runnable {
#Override
public void run() {
String a = Arrays.toString(control.temp);
System.out.println(a);
}
}
private void test() {
T1 t1 = new T1();
T2 t2 = new T2();
new Thread(t1).start();
new Thread(t2).start();
}
}
I heard about an interview question from one of my friend.
What happens when we pass a Runnable object to an object of a class extending Thread class and start the class.
public class A extends Thread {
Runnable obj;
public A(Runnable obj) {
this.obj=obj;
}
public void run() {
System.out.println("Printing A")
}
}
public class B implements Runnable {
public void run() {
System.out.println("Printing B");
}
}
public class MainApp {
public static void main() {
B b = new B();
A a = new A(b);
a.start();
}
}
Now it outputs Printing A
I was expecting Printing B as it is a perfect analogy to
Thread obj = new Thread(Runnable runnableObj)
Can someone please explain me this weird output??
Thread has a run method which essentially calls runnable.run(). Except that in your class A you have overriden that method to do something else.
So runnable.run() is not called any longer...
You are creating an instance of class A (Now it is a Thread).
Then you are calling start() of that Thread using instance a.
You are just passing a Runnable object as a normal instance variable to class A.
There is nothing complicated in that.
The result is normal.
With a tiny adjustment your code runs as expected.
public class A extends Thread {
Runnable obj;
public A(Runnable obj) {
this.obj = obj;
}
public void run() {
System.out.println("Printing A");
obj.run();// <---- I added this.
}
}
public class B implements Runnable {
public void run() {
System.out.println("Printing B");
}
}
public void test() {
B b = new B();
A a = new A(b);
a.start();
}
The problem is that you are overriding the run() method on Thread and never pass the Runnable obj to super(). Try this:
public class A extends Thread {
public A(Runnable obj) {
super(obj);
}
public class B implements Runnable {
public void run() {
System.out.println("Printing B");
}
}
public void test() {
B b = new B();
A a = new A(b);
a.start();
}
}
I want to use the the thread created on (say) instance x of class A in another class B.
I've stated my problems in better way in the form of comments below.
I have something like this:
Class A implements Runnable{
public static int num;
public void setNum(int i) { num = i; }
public int getNum() { return num; }
public void run(){
while(true){} //I want to keep this thread running continuously
}
}
Class B{
A a;
//I will call this method in class C to use the same instance of class A
public A getInstanceOfA() { return a; }
public static void main(String[] args){
a = new A();
Thread t = new Thread(a);
t.start();
a.setNum(5);
System.out.println(a.getNum()); //getting output as 5. Okay as Expected.
}
}
class C{
A a;
public static void main(String[] args){
a = getInstanceOfA();
System.out.println(a.getNum());
//Here I'm getting output 0 not 5 why? As Thread created on instance a is
//already running, and also I am using the same instance of class A
//so I should get the updated value 5, but getting 0. Why it is re-initializing num?
}
}
Please Help. Thanks.
I am not giving you the exact code but you should do it something like this.
Class B{
A a;
public B(){
initialize();
}
//I will call this method in class C to use the same instance of class A
public A getInstanceOfA() { return a; }
// this method should not be main
public void initialize{
a = new A();
Thread t = new Thread(a);
t.start();
a.setNum(5);
System.out.println(a.getNum()); //getting output as 5. Okay as Expected.
}
}
class C{
A a;
public static void main(String[] args){
B b = new B();
a = b.getInstanceOfA();
System.out.println(a.getNum());
//Here I'm getting output 0 not 5 why? As Thread created on instance a is
//already running, and also I am using the same instance of class A
//so I should get the updated value 5, but getting 0. Why it is re-initializing num?
}
}
You can also use Singleton
http://en.wikipedia.org/wiki/Singleton_pattern
Consider the following code:
class Chicks {
synchronized void yack(long id) {
for(int x = 1; x < 3; x++) {
System.out.print(id + " ");
Thread.yield();
}
}
}
public class ChicksYack implements Runnable {
Chicks c; //.....(1)
public static void main(String[] args) {
new ChicksYack().go();
}
void go() {
c = new Chicks(); //........(2)
new Thread(new ChicksYack()).start();
new Thread(new ChicksYack()).start();
}
public void run() {
c.yack(Thread.currentThread().getId());
}
}
When i run this code, I am getting a Null Pointer Exception that I have not initialized variable c. But didn't i initialized it at line ....(2)? I am having trouble getting this concept. Does threading has a part to play in this exception?
Look at this line:
new Thread(new ChicksYack()).start();
^^^^^^^^^^^^^^^^
The attribute c of the newly created ChicksYack object is never initialized. In the go() method you only initialize c for the current (this) object.
That's why you get an NPE in the run() method. A good solution would be to initialize that variable in a default constructor for ChicksYack.
In the go() method, you're instantiating two new ChickYack objects, which have a null c. You should put the c = new Chicks() in your ChicksYack constructor.
There are multiple instance of Class A that runs at a time.
Class A calls multiple instances of Class B in its run.
public Class Main {
public static void main(String args[] ) {
A a1 = new A();
Thread t1 = new Thread(a1);
t1.start();
A a2 = new A();
Thread t2 = new Thread(a2);
t2.start();
}
}
Class A implements Runnable {
public void run() {
B b1 = new B();
Thread t11 = new Thread(b1);
t11.start();
B b2 = new B();
Thread t21 = new Thread(b2);
t21.start();
}
}
There is method named "method" in class B where a Set Collection is edited. That edit is done based on static lock in Class B.
EDIT-
Class B implements Runnable {
private final static Object LOCK = new Object();
private final static Set<T> busyRecords = new HashSet<T>();
public void waitToWorkOn(final T obj) {
synchronized(LOCK) {
while (busyRecords.contains(obj)) {
LOCK.wait(); //go to sleep
}
busyRecords.add(obj);
}
}
public void doneWith(final T obj) {
synchronized(LOCK) {
busyRecords.remove(obj);
LOCK.notifyAll();
}
}
public void mathod(obj) {
try{
waitToWorkOn(obj);
.. do some work with obj
}
finally {
doneWith(obj);
}
}
public void run() {
method(getObj())
}
}
But that Set does not need concurrency control when it is accessed from different "A" instances. Only within an A instance, it needs to be locked for all B instances.
By this I mean, that when 2 instances of A are running, they should not be made to wait. But within an A instance if 2 B objects pick same obj, they have to wait inside LOCK.wait.
I don't think that LOCK can be made non-static as A calls multiple instances of B.Can we tune LOCK object here for better concurrency across A objects.
You can create a thread-safe instance of the shared collection and pass it to all the Bs for a given A.
Class A implements Runnable {
public void run() {
// create shared set instance scoped to A, and make it thread-safe
Set col = Collections.synchronizedSet(new HashSet());
B b1 = new B(col);
Thread t11 = new Thread(b1);
t11.start();
B b2 = new B(col);
Thread t21 = new Thread(b2);
t21.start();
}
}
Class B implements Runnable {
private final Set<T> someSet;
private B(Set<T> someSet) {
this.someSet = someSet;
}
public void method(final T obj) {
someSet.add(obj);
}
public void run() {
method()
}
}