Denis Anoykin

Greenhorn
+ Follow
since Jun 11, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Denis Anoykin

Usage example:
public class Test extends Thread {
public static class LockThread extends Thread {
Object lock;
intid;
public LockThread (int id, Object lock) {
this.lock = lock;
this.id = id;
}
public void run() {
System.out.println (id+" I will wait notification!");
synchronized (lock) {
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println (id+" I receive notification!");
}
}
public static void main(String strs[]) throws Exception {
Object lock1 = new Object();
Object lock2 = new Object();
LockThread t1 = new LockThread(1, lock1);
LockThread t2 = new LockThread(2, lock2);
LockThread t3 = new LockThread(3, lock2);
t1.start();
t2.start();
t3.start();
Thread.currentThread().sleep(200);
synchronized (lock2) {
lock2.notifyAll();
}
synchronized (lock1) {
lock1.notify();
}
}
}
The object of Float class contains member with primitive type 'float'
Thus I think in case:
new Float(Float.NaN).equals(new FLoat(Float.NaN))
Java compares float primitives into the Float objects
And in this case:
System.out.println(new Float(Float.NaN)==new Float(Float.NaN));
Java compares refernces to Float objects. And there are different references.
Hi,
It isn't good style to use hardcoded '\r' or '\n'.
You should call method:
System.getProperty("line.separator")
It returns char sequence that uses on your platform as line separator.
I agree that #4 answer on question #1 isn't right formulated. But in this answer I see keywords 'wait()' and 'notify()'.
I can't understand how we can use other methods (in other answers)?
I see that some persons suggest that #1 answer is right. Would they explain this more detailed?
I think that right answer is 'False'
My example:
public class Test {
public void a() {}
public int a(int b) { return 0;}
public double a(double b) {return 0;}
}
This code is compiled.
Paul,
It is great hint about static final modifier
In context of this question maybe you can clear next moment:
Why we can't use not-final static members in non-static inner classes?
I know that it is declared in the lang spec. But why it is necessary? I can't find any cause of this fact. Only one that I can suppose that it is lang spec authors fancy.
Could you explain this moment more detailed (cause of this rule and example with Java code)?
Hi
About answer #5
What is syntax of anonymous static classes?
We can create instance of static inner class using full name of class only. I think this is cause why we can't create static inner class.
This is example of anonymous class definition syntax:
Runnable r = new Runnable () {
public void run() {
int p=0;
}
};
Jeena and Farhan
For question #1 I can't understand what you mean when you suggest to use ThreadGroup object. I created sample where each thread is in different ThreadGroup and these threads execute synchronized method simultaneously. Why answer #4 for question #1 is incorrect?
public class Test {
public Test() {
}
public static void main(String strs[]) throws Exception {
Test t = new Test();
Thread t1 = t.new ThreadWait("Thread 1");
Thread t2 = t.new ThreadWait("Thread 2");
Thread t3 = t.new ThreadWait("Thread 3");
Thread t4 = t.new ThreadNotifyAll("Thread 4");
System.out.println ("Thread Groups for threads:");
System.out.println (t1.getThreadGroup());
System.out.println (t2.getThreadGroup());
System.out.println (t3.getThreadGroup());
System.out.println (t4.getThreadGroup());

t1.start();
t2.start();
t3.start();
Thread.currentThread().sleep(200);
t4.start();
}
public class ThreadNotifyAll extends Thread {
public ThreadNotifyAll(String str) {
super(new ThreadGroup(str), str);
}
public void run() {
threadNotifyAllMethod();
}
}
public class ThreadWait extends Thread {
public ThreadWait(String str) {
super(new ThreadGroup(str), str);
}
public void run() {
threadWaitMethod();
}
}
public synchronized void threadNotifyAllMethod() {
System.out.println(Thread.currentThread().getName() + " notifyAll - (1)");
this.notifyAll();
System.out.println(Thread.currentThread().getName() + " notifyAll - (2)");
}
public synchronized void threadWaitMethod() {
System.out.println(Thread.currentThread().getName() + " wait - (1)");
try {
wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " wait - (2)");
}
}