Cleared SCJP 86%

I finally cleared SCJP and closing this blog

as a last post I would mention what I had gone through

the exam was hectic although I saved a lot of time
everywhere I read it is easy probably because whoever said so have gone through several mocks, in my case I do not want to ruin first time experience so I did not give any mocks although 2 came free with my SCJP(Kathy and Birt ) book.

the exam would seem easy for if you have gone through their book(exhaustively).

for brevity and to not misguide you I' ll let this post close because any information can be misinterpreted(and I mean in a wrong way)

Promentric testing location aka SCJP testing location Allahabad UP on Google Map

This is the link of Prometric testing center of Allahabad , the sole center which conducts SCJP exam in Allahabad(the website shows two but they are one and the same center)

Click Here

Managing join function on threads

I spent 2 hours trying to figure out how to join a thread at the end of another thread and none of these two is the 'main' thread, after a some miss-and-trials I came up lucky enough with a solution , I know it's jibberish but it works(frankly I expected it to work the other way round but these are the intricacies of OOP)

this is the code.

class MyThread extends Thread{
public void run(){
for(int i=1;i<=500;i++){
System.out.printf("%d %s\n",i,Thread.currentThread().getName());
//try{
// Thread.sleep(50);
//}catch(Exception e){}
}
}
}



public class MyRunnable implements Runnable{

public MyRunnable() {

}

public void run() {
MyThread t= new MyThread(); // added to check join function
t.start();
t.setName("Thread");
try{
t.join();
}catch(Exception e){}

for(int i=1;i<=500;i++){
// try {
//Thread.sleep(5);
// }catch(Exception e){}
//if(i==250)
// t.join();

System.out.printf("%d %s\n",i,Thread.currentThread().getName());
if(i%10==0)Thread.yield();
}
}
}




public class MainClass {
public static void main(String args[])throws InterruptedException{
// MyThread t= new MyThread();
MyRunnable mr = new MyRunnable();
System.out.println("master thread is "+Thread.currentThread().getName());
Thread thread = new Thread(mr);
thread.setName("*runnable");
//t.setName("thread");
//t.setPriority(8);
//thread.setPriority(8);
thread.start();
// t.start();
// t.join();
//t.setPriority(Thread.MAX_PRIORITY);
//t.start(); will throw illegal thread state exception
}
}

the point is the runnable thread sticks to end of 'thread' some intricacy of being a static method but I am not getting a logical answer out of my grey cells, getting back to dwell more.

fixing thread behaviour - novice

consider these three classes,


public class MyRunnable implements Runnable{

public MyRunnable() {
}

public void run(){
for(int i=1;i<=50;i++){
try {
//Thread.sleep(5);
}catch(Exception e){}
System.out.println(Thread.currentThread().getName());

}
}

}



public class MainClass {
public static void main(String args[]){
MyThread t= new MyThread();
MyRunnable mr = new MyRunnable();
System.out.println("master thread is "+Thread.currentThread().getName());
Thread thread = new Thread(mr);
thread.setName("runnable");
t.setName("thread");
thread.start();
t.start();
t.setPriority(Thread.MAX_PRIORITY);
//t.start(); will throw illegal thread state exception
}
}


class MyThread extends Thread{
public void run(){
for(int i=1;i<=50;i++){
System.out.println(Thread.currentThread().getName());
try{
// Thread.sleep(50);
}catch(Exception e){}
}
}
}


the code is simple but I wrote this code to let thread named "thread" first execute followed by Runnable implemented thread but it all go wrong since I first started thread 'runnable' so it will execute few lines of codes before starting thread 'thread' and my code goes awry, to fix this just uncomment this line

//Thread.sleep(5);

this passes transfers 'runnable' to sleep state(or blocking state) which gives 'main'(yeah!! main is also a thread) thread to start second thread and since it is higher priority so runs to completion till the before returning back to 'runnable'

A little threading experiment

Consider the following program

public class MainClass {
public static void main(String args[]){
MyThread t= new MyThread();
MyRunnable mr = new MyRunnable();
System.out.println("master thread is" +Thread.currentThread().getName());
Thread thread = new Thread(mr);
thread.start();
t.start();
thread.setName("runnable");
t.setName("thread");
//t.setPriority(Thread.MAX_PRIORITY);
//t.start(); will throw illegal thread state exception
}
}


this code works on classes posted in previous posts(MyThread class and MyRunnable class with only loop running 50 times) and it runs cool with showing variable output, if we run this code then we might get something like an output showing

master thread is main
Thread-1
thread

thre...
......

this Thread-1 is generic name of MyThread class supplied by JVM which shows once loop is executed before reaching the setname function, to fix this up(nothin is guaranteed so this fix might not work but we are just taking extra precautions) transfer the setname function above the code for starting both threads.
.................................................
Thread thread = new Thread(mr);
thread.setName("runnable");
t.setName("thread");
thread.start();
t.start();
//t.setPriority(Thread.MAX_PR.......


which fixes up the mess



Thread in windows

It seems windows thread mechanism and scheduler is rather lazy or The our lab systems are too powerful (well they are , a 2GB RAM with core 2 quad Intel platforms at it's heart) so I write a simple program to check the behaviour of threads, I implemented simple code including three classes a simple class extending thread interface , a class implementing runnable interface and a main class to keep everything going

following are the code of each of the three classes.

public class MainClass {
public static void main(String args[]){
MyThread t= new MyThread();
MyRunnable mr = new MyRunnable();
Thread thread = new Thread(mr);
t.start();
thread.start();

}
}



public class MyRunnable implements Runnable{

public MyRunnable() {
}
public void run(){
for(int i=1;i<=5000;i++){
System.out.println("runnable is running");
}
}
}



class MyThread extends Thread{
public void run(){
for(int i=1;i<=5000;i++)

System.out.println("The extended thread is running");
}
}


take note of the heavy loop I created in both threads , it is not something to hog your CPU cycles(well it does so) but if I decrease the value by one zero the the ouput seems not of any thread importance , if I reduced loop to 500 , I got 500 lines of "The extended thread is running" followed by 500 lines of "runnable is running" , it seems that either the JDK implementation in windows doesnot like thread much or the underlying thread handling in windows system(if JVM maps thread handling to underlying system) is awkward, however the above code show me some real grace with first "runnable is running" after 638 lines of "The extended thread is running" , that's just an observation, everything else works fine.

SCJP success experience of Sujan Surendran (as posted on javaranch)

In the afternoon on Saturday the 22th of June, I began making telephone calls to Prometric centers in the city to book a slot for SCJP test for the weekend ahead, 28th or 29th of June (or in the worst case any date before the expiry date on my SCJP coupon, 30th of June). I had got the coupon some eight months before and had forgotten about it for a long time (Ever since I started working, I've forgotten a lot of things of my student life). Unfortunately slots for the weekend were all taken and none were available through out Bangalore. In the end the farthest date before 30th that I could get, was 25th. I got a 3.30 pm afternoon slot. Time was too short and I had to revise a lot of things. I was in a kind of do-or-die situation, and I worked quite hard.


On the morning of 25th I woke up with flu. In the afternoon I left my office a few minutes after lunch so that I'll be on time at the Prometric center. I felt a bit uncomfortable during the test because of sinusitis. But thank God, I had flu on day on which I had the test. Had the flu come before the test date I wouldn't have been able to prepare for the test. I passed SCJP 1.5 with 97%. I was a bit like heart-broken initially for not being able to get 100, but then I was satisfied with what I have got. With only a short time before the test for preparation and flu, 97 sounded like a good score.

Although the two day revision before the test was crucial, the secret to my success is the interest I have in Java since my college days. I always found it very exciting to work on Java. After college, before I started working for a company I spent most of my time working on small Java applications and thought of getting Sun Certification. I also spent one full day at book stores to find the best exam guide. The SCJP study guide (K&B) by Kathy and Bert is a wonderful book not just for SCJP but even otherwise it is an excellent reference/study material for Java developers. I have always loved Java and the book adds to the beauty of Java and makes it a pleasant experience to read through. Thank you, Kathy and Bert.

Regards,
Sujan

This is the success story of Sujan Surendran as posted in javaranch, for the post click here, the person got 97%in his exam...


'instanceOf' is not a keyword

instanceOf which bumps everywhere when somebody prepare for SCJP 5 is not a keyword rather it can be used as a normal variable name

These all are legal

class instanceOf{}

void instaceOf{<parameter list>}

in both of these example it acts like a normal keyword like foo or bar (or Mesopotamia for Christ's sake)

however instanceof is a keyword

for discussion refer this javaranch thread

[Caption]

The Wierd nature of postfix operator

while on javaranch today I found a post regarding a question which let everybody's brain fuzzy preparing for java certification (or say SCJP), the wierd behaviour of postfix operator

what is expected by the following code

int x = 1;
x = x++;
System.out.println(x);
as most of beginners my immediate respnse was 2 but then with help of some ranchers and few digging in Java language specification I found that this is not the case, this code prints 1, in fact postfix can be think of like this method

int postfixIncrement() {
int result = x;
x = x + 1;
return result;
}
where it returns original value, so if you typex
x=x++;
what hapens is x++ is calculated so x is incremented and stored in
a temorary variable and then it returns original(not incremented x's value
which in this case is assigned to x itself so the new value of x is overwritten by
x's original value,
for example observe this code
in x=1;
int y = x++;
System.out.println(x);


here x is not overwritten so it will print 1 and y's value is 2(as expected or rather say unexpected)

reference:rizzoweb

and for detailed explanation refer JLS(Java Language Specification)

containsAll clarification fro SCJP

Collection:containsAll (Programmer Certification (SCJP) forum at JavaRanch)
If you look at the Collection interface, the containsAll method has signature:

code:

public boolean containsAll(Collection c)

therefore you can check against any type of collection that contains Objects. It could (but doesn't) limit the collection to the E type parameter.
It also says that the ClassCastException is optional so therefore dependent upon the implementation.

Incidentally, the containsAll method in ArrayList is inherited from the AbstractCollection so you may want to look there for more info.

One way you could think about the implementation is like so (you will be able to find the actual source somewhere):

code:

for(Object o:c){
if(!this.contains(o){
return false;
}
}
return true;

source : javaranch
forum : SCJP

SCJP journey © 2008. Template by Dicas Blogger.

TOPO