Runnable Inline Class declaration - able to access outer non-final variables - java

In the code below, I am wondering why the run() in the inline class is able to access the outer class variable - semaphore (even though it is not declared final).
private Semaphore semaphore = new Semaphore(bufferSize);
private Runnable producerRunnable = new Runnable(){
#Override
public void run()
{
try
{
semaphore.acquire();
}
catch(InterruptedException e)
{
System.out.println("producer was interrupted while trying to acquire semaphore");
}
}
};
I expected Eclipse to flag a compilation error - since semaphore reference is not declared final

This is normal. The final requirement is only described to apply to local variables and parameters (JLS):
Any local variable, formal parameter, or exception parameter used but not declared in an inner class must either be declared final [...].
Members of the enclosing instance don't need to be final to access them.

Because you have defined semaphore as a global variable to that class and all the local variables should be declared as final if you want to access it in the runnable .
public class KamalTest {
/**
* #param args the command line arguments
*/
private Semaphore semaphore = new Semaphore(2);
private Runnable producerRunnable = new Runnable() {
#Override
public void run() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
System.out.println("producer was interrupted while trying to acquire semaphore");
}
}
};
public void startThread1() {
Thread th = new Thread(producerRunnable);
th.start();
}
public void startThread2() {
final Semaphore semPhore = new Semaphore(2);
Runnable runnable = new Runnable() {
#Override
public void run() {
try {
semPhore.acquire();
} catch (InterruptedException ex) {
Logger.getLogger(KamalTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
Thread th = new Thread(runnable);
th.start();
}
public static void main(String[] args) {
KamalTest kt = new KamalTest();
kt.startThread1();
System.out.println("Internet Available : " + isInternetAvailable());
}
}
}

Related

Java FixedThreadPool with resources per thread?

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();
}
}

How to restrict access to a method by a specific thread?

In an interview I was asked to come up with an approach which will ensure that while thread T1 and T3 can access a method of a class, T2 cannot access the method.
I am unable to provide any solution to this. Could you please provide an example with an explanation?
I have later come up with the following solution. Is it efficient?
package JavaProgramming;
public class EligibleThread implements Runnable {
public void method1() {
System.out.println("Hello");
}
public static void main(String[] args) {
EligibleThread t1 = new EligibleThread();
EligibleThread t2 = new EligibleThread();
Thread t11 = new Thread(t1, "t1");
Thread t22 = new Thread(t2, "t2");
t11.start();
t22.start();
}
public void run() {
if (Thread.currentThread().getName() != "t2") {
method1();
} else{
try {
throw new Exception("Access is denied");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
You can use protected modifier, like following code. T1 can call aMethod() by extending Main class, but T2 can't call aMethod().
public class Main {
protected void aMethod() {
}
}
class T1 extends Main implements Runnable{
#Override
public void run() {
aMethod();
}
}
class T2 implements Runnable{
#Override
public void run() {
// here can't call Main.aMethod()
}
}

singleton and thread do not work together

I have a singleton class which is also Runnable (and Observable). When I'm trying to start the thread from main, it does not create a new thread and gets stuck in the main loop.
public class A extends Observable implements Runnable
{
private static A instance = null;
private A() {
}
public static A getInstance() {
if(instance == null) {
instance = new A();
}
return instance;
}
#Override
public void run()
{
while (true) {
System.out.print("\nSleeping");
// sleep
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.print("\n[Error]");
}
}
}
}
and the main class:
Thread thread = new Thread(A.getInstance());
thread.run();
System.out.print("\nAfter...");
I cant see the "After" log, just the "Sleeping" in a loop. Why?
You're calling run(), instead of start().

Access anonymous outer class without storing in variable?

Is there a way to access an anonymous outer class? A normal class can be accessed by ClassName.this. This doesn't work, as an anonymous class obviously doesn't have a name. I also tried using the extended class/interface (like Runnable.this) but it doesn't seem like it would work this way.
I'm sure this may be not the best coding style, I'm just curious if it's possible without storing this of the outer in a variable.
Example, watch out for outer.this:
public class A
{
public static void main(String[] args) {
new Thread(new Runnable() {
#Override
public void run() {
new Thread(new Runnable() {
#Override
public void run() {
synchronized (outher.this) {
outher.this.notify();
}
}
}).start();
try {
synchronized (this) {
wait();
}
} catch (final InterruptedException ex) {}
}
}).start();
}
}
No, there is no way to access anonymous classes from anywhere, except from inside them (i.e. otherwise than by this reference). Or by an explicitly declared variable.
final Runnable r1 = new Runnable() {...};
Runnable r2 = new Runnable() {
public void run() {
synchronized(r1) {...}
}
};
You could add a method to return this middle this. It would be in scope but not hidden (is that the right term? Shadowed? I forget.).
public static void main(String[] args) {
new Thread(new Runnable() {
Runnable middleThis() { return this; } // <-- this
#Override
public void run() {
new Thread(new Runnable() {
#Override
public void run() {
synchronized (middleThis()) {
middleThis().notify();
Note, although anonymous inner classes have no name, they still are types. So adding members is visible to the immediate expression (new X() { Y z; }.z) and inside. You can't do middleThis().middleThis().

How can I run thread from Main method in Java application?

I believe variables used in static main method should be also static as well.
The problem is that I cannot use this in this method at all. If I remember correctly, I have to initiate thread with commnad myThread = new ThreaD(this).
The below codes produces an error because I used this in thread initiation.
What have I done wrong here?
package app;
public class Server implements Runnable{
static Thread myThread;
public void run() {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
System.out.println("Good morning");
myThread = new Thread(this);
}
}
You can't use this because main is a static method, this refers to the current instance and there is none. You can create a Runnable object that you can pass into the thread:
myThread = new Thread(new Server());
myThread.start();
That will cause whatever you put in the Server class' run method to be executed by myThread.
There are two separate concepts here, the Thread and the Runnable. The Runnable specifies what work needs to be done, the Thread is the mechanism that executes the Runnable. Although Thread has a run method that you can extend, you can ignore that and use a separate Runnable.
Change new Thread(this) to new Thread(new Server()):
package app;
public class Server implements Runnable{
static Thread myThread;
public void run() {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
System.out.println("Good morning");
myThread = new Thread(new Server());
}
}
class SimpleThread extends Thread {
public SimpleThread(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " thread: " + getName());
try {
sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {}
}
System.out.println("DONE! thread: " + getName());
}
}
class TwoThreadsTest {
public static void main (String[] args) {
new SimpleThread("test1").start();
new SimpleThread("test2").start();
}
}

Categories

Resources