Alvaro San Millan

Ranch Hand
+ Follow
since Mar 31, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Alvaro San Millan

Thanks for your answer. I am working on that
12 years ago
Hello everyone,

I have to load information from an Employee contained in a text file (one field per line), and convert it to a XML or JSON representation MATCHING a schema provided. And this last part it's what I am not sure. I know to load the data, and convert it to XML and also to convert from XML to JSON using XSLT, but how about to matching the schema?

Thanks in advance
Hello everyone,

I am quite new with RESTful web services (not with SOAP-based WS), but I have been reading and researching a lot. The point is that there are a lot of tools and frameworks and I am confused.

I have to build a RESTful web service that retrieve an Employee resource in XML and JSON formats. Data for the Employee resources are given in txt files and the schema for the XML response "employee.xsd" is also given.

I have to use Java.

Any idea where can I start from?

Thanks in advance
12 years ago
Hi Emilio,

I was obsessive about this question. Paul Clapham clarified me the answer E when he wrote this:

If your application starts a thread, then the operating system will execute that thread according to its rules. Which normally means that the thread will start running right away. But it's possible that the operating system is already running several threads which have higher priority than yours, so it won't start running your thread right away. Eventually your thread will run, when those higher-priority threads have gone away, but there isn't any guarantee about how long that will take. It could be seconds, or hours, or weeks, or longer.

So: yes, it's guaranteed that a thread will run. But it isn't guaranteed when that will happen, and it might never happen.



To understand answer E we have to assume that "Andi" thread never happen, for example, assume that the operating system is running several threads which have higher priority than yours and your thread never have an opportunity to be executed. In fact, this is really difficult to happen with the modern operating systems, but for the propose of the exam it can happen. To be honest, I think that option B can be correct too if we make the same assumption.

Maybe someone else can clarify a little more.


Have a look this thread:

https://forums.oracle.com/forums/thread.jspa?threadID=2260228&tstart=0
Hi guys,

Finally, I passed this exam with the score I was looking for (90% to 100%). I am going to share my experience because maybe it will be useful to someone in the same situation.

In November 2007 I gave up my job. I was working in a European project and I realized that my English was terrible, actually it was worse. Yes, English is not my best subject . So I went to Australia to improve it. In that moment I had almost 3 years of experience in J2EE and I had passed Sun Certified Programmer for the Java Platform 1.4 with 73% (2006).

With this scenario, I was in Australia for 2 years and a half. The global financial crisis was there too, and with my visa it was impossible to get a job in IT. So I came back to London in September 2010 and tried to get a job in IT as a Java Developer but it was quite difficult too, my knowledge about Java platform was not clear. For this reason I decided to pass this exam to have my Java Core knowledge up-to-date.

Last year I have been working, not in IT, and preparing this certification. I have studied quite hard to get this score. I am not a genius, this score is the result of a lot of effort. Frankly, I was obsessive about getting this mark. I used two books:

- SCPJ6 - Study Guide (310-065) - Kathy Sierra and Bert Bates
- SCPJ 6 - Programmer Practice Exams - Kathy Sierra and Bert Bates

Also, I used two simulators:

- ExamLab
- Whizlabs

I wrote a lot of small programs without any IDE.

This upgrade 1Z0-852 exam has 46 questions and 150 minutes (August 2011). You need 58% to pass. I always thought that this exam would be more difficult than the 1Z0-851 because it is for certified Java programmers. Honestly, I would not say that the exam is easy, but for me it was. I finished all the questions in 80 minutes and I had doubts in two (about cohesion, coupling and encapsulation). I used the rest of the time to review, review and review and try to answer these two questions. I did not change any of my answers and I answered those questions. Probably, I could get this score few months ago, but in June I found the ExamLab exams and I got in the first exam 71%. I was really upset with myself. I could not believe it. I thought that I had everything under control and that score shocked me. So I started to repeat all questions in the K&S Study Guide, Whizlabs and ExamLab simulators. The problem was that I had memorized many of the questions. So I got K&S Programmer Practice Exam book and when I started to get around 90% to 100% in all these mocks exams (that happened the second time I did the exams), I went to pass the certification. It sounds crazy I know, but I learnt a lot, much more than someone needs to pass this exam. Now I have a solid knowledge about the Java language and the core API’s. I have to say that the ExamLab and K&S Programmer Practice exams are much more difficult than the real exam.

And this has been my experience. Now, I would like to find a job and at the same time I prepare the Oracle Certified Expert, EE 6, JSP and Servlet developer.

I would like to thank everyone in these forums who spends their time to help us. Also to K&S, they wrote a good books for passing this certification, and also to the ExamLab team, they have a good simulator, quite hard, but really good.

Good luck on your exams!!!
12 years ago
We cannot be sure about those objects have been collected or not. It is the JVM which execute the GC process, and maybe this process never happen in our code. Also, if we invoke it (which is not recommended) by System.gc() or Runtime.getRuntime().gc() methods we are not sure if the process will be launched or not.
Hi guys,

I have a problem with this question:



When line 8 is reached, which are true? (Choose all that apply.)

A. a1.acctNum == a3.password
B. a1.password == a2.password
C. Three objects are eligible for garbage collection.
D. Four objects are eligible for garbage collection.
E. Six objects are eligible for garbage collection.
F. Less than three objects are eligible for garbage collection.
G. More than six objects are eligible for garbage collection.

And this is the answer in the book:

Answer (for Objective 7.4):
- B and D are correct. B is correct, although when line 8 is reached, the references are lost.
D is correct because only one Account object is created, and a1, a2, and a3 all refer to it.
The Account object has two Long objects associated with it, and finally the anonymous
Banker object is also eligible.
- A, C, E, F, and G are incorrect based on the above.



Option B is difficult to understand. When line 8 is reached all references are lost. Well, I will believe this because I think the author wanted to ask if both 'password' references point to the same object in the heap.

My worry is with option D. I think there are 5 objects eligible for garbage collection and not 4. The Account object, one Long created in line 12 using constructor Long(String) (the String is in the pool, so is reachable in line 8), one more Long created by boxing in line 15 , another Long created by boxing in line 16 and the Banker object.

What do you think?

Thanks in advance,

Alvaro
Hi again,

When I was writing this thread I also asked this question in Oracle forums and I got a few answers. Check this link

http://forums.oracle.com/forums/thread.jspa?threadID=2260228&tstart=0

I think Paul is right in his comment in the context of the certification exam. But in real life, that situation is difficult to happen as some person affirms in the Oracle forums.

Once again, thanks for all your comments.
Thanks Tommy for the example. For me is clear now
Thank you very much Paul and the rest of you for your opinions. Finally, I have understood it.

I am sorry for opening a new thread in other forum about the same topic, but I thought that "Threads and Synchronization" forum was more suitable for this question.

Summary:

Is it guaranteed that a started thread will run to completion?

As K&S wrote in their study guide:

Don't make the mistake of designing your program to be dependent on a particular implementation of the JVM. As you'll learn a little later, different JVMs can run threads in profoundly different ways. For example, one JVM might be sure that all threads get their turn, with a fairly even amount of time allocated for each thread in a nice, happy, round-robin fashion. But in other JVMs, a thread might start running and then just hog the whole show, never stepping out so others can have a turn. If you test your application on the "nice turn-taking" JVM, and you don't know what is and is not guaranteed in Java, then you might be in for a big shock when you run it under a JVM with a different thread scheduling mechanism.



If your application starts a thread, then the operating system will execute that thread according to its rules. Which normally means that the thread will start running right away. But it's possible that the operating system is already running several threads which have higher priority than yours, so it won't start running your thread right away. Eventually your thread will run, when those higher-priority threads have gone away, but there isn't any guarantee about how long that will take. It could be seconds, or hours, or weeks, or longer.

Yes, it's guaranteed that a thread will run. But it isn't guaranteed when that will happen, and it might never happen.


So, the answer is NO.





Thanks a lot for your comment. It is really useful.

Don't make the mistake of designing your program to be dependent on a particular implementation of the JVM. As you'll learn a little later, different JVMs can run threads in profoundly different ways. For example, one JVM might be sure that all threads get their turn, with a fairly even amount of time allocated for each thread in a nice, happy, round-robin fashion. But in other JVMs, a thread might start running and then just hog the whole show, never stepping out so others can have a turn. If you test your application on the "nice turn-taking" JVM, and you don't know what is and is not guaranteed in Java, then you might be in for a big shock when you run it under a JVM with a different thread scheduling mechanism.



I understood from the sentence in bold that a thread (in some Java Virtual Machine implementations) can get the running state and never step out. Well, I can think different possibilities that a thread will keep the CPU forever (in some Java Virtual Machine implementations), for example, a thread which target Runnable has an endless loop in its run() method. If this thread gets the running state in this kind of Java Virtual Machine implementations, it will keep the CPU forever.

So, there is not guaranteed that a started thread will run to completion in all Java Virtual Machine implementation.

Again, I repeat to myself

THERE IS NOT GUARANTEED THAT A STARTED THREAD WILL RUN TO COMPLETION IN ALL JAVA VIRTUAL MACHINE IMPLEMENTATIONS.

Well, but in MY EXAMPLE, can this situation happen? All the four threads (main thread, one, two and three) are FINITE. So, if one of these four threads, for example, thread "three" gets the running state in this kind of Java Virtual Machine implementations and keep the CPU so others can not have a turn, in some moment the thread "three" will finish its run() target method (in this example is only to print out a String) and it will leave the running state, so other thread can have a turn, and all of them will run to completion (in this example)

Am I right?
Hi guys,

Someone wrote me this about threads:

"Thread running depends on the VM Scheduler and there is NO guaranteed a Thread will run and pick by the VM Scheduler despite you have started the Thread with a call x.start(). When you started the Thread it's just in a RUNNABLE STATE, reread the chapter until you fully understood"

(Chapter 9 Thread - Sun Certified Programmer for Java 6 - Study Guide (310-065) - Kathy Sierra and Bert Bates - 2008)

It is the red sentence that make me really confused. Actually, I read that chapter several times, and I could not read this kind of affirmation in all the chapter. What I could read is this,

From the book (pages 713 and 714)



And the authors wrote this:

Nothing is guaranteed in the preceding code EXCEPT this:

Each thread will start, and each thread will run to completion.



I try to explain myself, what I understood from the sentence in the book: Each thread will start, and each thread will run to completion

$java ManyNames

After this call, main method from the class ManyNames is invoked and the JVM execute the code in that method sequentially. When the main thread calls one.start(), the one thread (Java thread) is mapped with an Operative System thread (well, in most implementations), but the important thing, the one thread (Java thread) gets the RUNNABLE state waiting for a chance to get the RUNNING state an start the execution of the run() method of its target Runnable. So, at this point we have two thread that can be chosen to get the RUNNING state, main thread and the thread one. Assume main thread executes main method to the end. In this case, the calls two.start() and three.start() are invoked. Main thread finishes, and we have three alive threads waiting for its turn to get the RUNNING state. For me run to completion means that all these threads will print three times the sentence:

"Run by [THREAD NAME: Fred, Lucy or Ricky], x is [1, 2 or 3]"

And all of them will finish their run() method (AS THE CODE STANDS). One possible output could be:
Run by Fred, x is 1
Run by Lucy, x is 1
Run by Lucy, x is 2
Run by Fred, x is 2
Run by Lucy, x is 3
Run by Fred, x is 3
Run by Ricky, x is 1
Run by Ricky, x is 2
Run by Ricky, x is 3

But this order is not guaranteed.

So, my question is simple, if we execute THIS CODE (without any changed) in ANY Java Virtual Machine, is it GUARANTEED that all started threads (main thread, Fred, Lucy and Ricky) will run to completion OR I have to believe what the guy wrote me: there is NO guaranteed a Thread will run and pick by the VM Scheduler despite you have started the Thread with a call x.start()?

If what this guy said is correct, is it worth create and start a thread if there are not guaranteed that this thread will run to completion? Well, if it is a nice visual clock on the screen it does not matter if we can see the clock or not, but how about if it is a thread that control the emergency process in a factory, just an example.


Thanks for your opinions,

Alvaro

Thanks Tommy for your quick answer. Please, could you read my questions/comments to your comments.
I wrote them in blue colour with (CAPITAL LETTERS) to find them easier.

public class Race
{
public static void main(String[] args)
{
Horse h = new Horse();
Thread t1 = new Thread(h, "Andi");
Thread t2 = new Thread(h, "Eyra");

new Race().go(t2); //(1) - This call will always execute first. (I AGREE)

t1.start(); // (2) - This call will execute second, but doesn't mean t1 thread is execute just t1 is alive in state of execution.
/(I AGREE, BUT IT IS IN THIS POINT WHERE I GET CONFUSED. THERE IS NOT
// GUARANTEED THAT IT WILL RUN IMMEDIATELY, BUT WILL IT RUN TO COMPLETION?
// IF YOUR ANSWER TO MY QUESTION IS YES, THE BIG QUESTION ABOUT IF THE OPTION "E"
// IS A POSSIBILITY CAN BE ANSWER RIGHT NOW:
//
// E. The output could be: "Eyra ", followed by an exception.
//
// I THINK THIS OPTION NEEDS THE WORD "Andi " SOMEWHERE IN THE OUTPUT TO BE CORRECT
// AM I RIGHT?


t2.start(); // (3) - This call will execute third, but doesn't mean t2 thread is execute just t2 is alive in state of execution.
// (I AM NOT SURE WHAT YOU MEANT WHEN YOU SAY ...just t2 is alive in state of execution.
// I THINK THIS IS THE CALL WHICH WILL THROW THE EXCEPTION, BECAUSE THERE HAVE ALREADY BEEN
// A CALL TO t2.start() (WELL, TO t.start()) IN THE METHOD go() IN (1)
// AM I RIGHT?)

}

void go(Thread t) // No LOCK is acquired for this method so, any thread can jump and interrupt t thread
{ // (I AGREE, BUT AS THE CODE STANDS THERE IS NOT A POSSIBLE INTERRUPTION
// AM I RIGHT?)


System.out.println("go before"); //This line execute sequentialy and print out

t.start(); // This line started the thread of execution, but not guaranteed it will run.
// (BUT WILL IT RUN TO COMPLETION?
// AM I RIGHT?)


System.out.println("go after"); //This line execute sequentialy and print out
}
}

class Horse implements Runnable
{
public void run()
{
System.out.print(Thread.currentThread().getName() + " ");
}
}

/*


1st run:

go before
go after
Eyra Exception in thread "main" Andi java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at Race.main(Race.java:10)


new Race().go(t2); //(1) - Execute first calling the go(t2) method

//(1a) - The go(t2) method is executed
//(1b) t.start(); - Could execute first or second or third or not executed at all.
// WHAT DO YOU MEAN WHEN YOU SAY: ... or not executed at all?
// IS THERE ANY POSSIBILITY THAT THE run() METHOD IN THREAD t2 "Eyra " WILL
// NOT BE EXECUTED?

//This thread guaranteed to execute only the go() method acquired a LOCK
//on the go() method with a Synchronized keyword. Otherwise, t1 or t2 code
//lines below run next.
// (I AGREE, BUT THE CALL TO t.start() IS DONE AT THIS POINT, SO
// THE THREAD t2 IS AN ALIVE THREAD AND IT WILL RUN TO COMPLETION
// AM I RIGHT?


t1.start(); //(2) - Execute first or second or third or won't execute at all.
t2.start(); //(2) - Execute first or second or third or won't execute at all.
// I AM SORRY TO REPEAT THE SAME QUESTION, BUT HERE I STILL
// CANNOT UNDERSTAND HOW A CALL LIKE t1.start() IN THE MAIN THREAD WILL
// NOT BE EXECUTED?
// ALL THESE CALLS ARE IN THE MAIN METHOD AND THEY WILL BE EXECUTED
// SEQUENTIALLY INSIDE THE MAIN THREAD WHEN IT IS RUNNING
// AM I RIGHT?


Based on the result above you can see that t2 is executed twice before t1 does. t2.start() or t.start()
execute FIRST and t1.start(); execute SECOND and print out "Andi" before the rest of exception message
print out.

So this conclude and confirm that:

E. The output could be: "Eyra ", followed by an exception.

(AND THIS IS WHAT I CAN NOT UNDERSTAND, IF THREAD t1 PRINTED OUT AS YOU SAID ABOVE (IN BOLD RED STYLE),
THE WORD "Andi " IS MISSING IN THIS OUTPUT, IT MUST APPEAR SOMEWHERE IN THE OUTPUT TO MAKE THE OPTION CORRECT.
AM I RIGHT?)


---------

2nd run:

go before
go after
Eyra Exception in thread "main" java.lang.IllegalThreadStateExceptionAndi
at java.lang.Thread.start(Unknown Source)
at Race.main(Race.java:10)

(THE WORD "Andi " APPEARS AGAIN IN THE OUTPUT)

- This scenario t.start() or t2.start() started first or second, and t.start() didn't start at all. From
this experiment you can confirm that no Thread is guaranteed to run.

(AS YOU CAN SEE IN THIS OUTPUT THREAD t2 PRINTED OUT ITS NAME "Eyra ",
THREAD t1 PRINTED OUT ITS NAME "Andi " TOO, AND THE MAIN THREAD HAS THROWN THE EXCEPTION)


---------

3rd run:

go before
go after
Eyra Andi Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at Race.main(Race.java:10)


- In this case t.start() or t2.start() execute FIRST and followed t1.start(), and finally an exception
thrown by t.start() or t2.start().


---------
4th run:

go before
go after
Eyra Exception in thread "main" java.lang.IllegalThreadStateExceptionAndi
at java.lang.Thread.start(Unknown Source)
at Race.main(Race.java:10)

- This scenario similar to 1st run except the "Andi" print out in different place within the exception
message print out.

Because is no LOCK on an Object any thread can jump in and execute while other thread is running
(i.e t.start() or t2.start()) interrupted by t.start() despite the FACT that each thread will
run to it completion and it is in this case.
(I DO NOT REALLY UNDERSTAND WHAT YOU MEAN WITH THIS SENTENCE. I CAN SEE
THAT THE run() METHOD IS NOT SYNCHRONIZED, SO SEVERAL THREADS CAN JUMP IN AND EXECUTE
ITS CODE CONCURRENTLY, BUT WHAT YOU MEAN WITH THE REST OF THE SENTENCE PARTICULARLY
WHERE YOU HAS USED THE WORD "INTERRUPTED". IT IS NOT CLEAR TO ME.)



The running thread is interrupted by other thread, but it will running until complete it execution once
it has a chance enter the running state.

(WHEN YOU SAY "INTERRUPTED" YOU MEAN THAT THE RUNNING THREAD GOES TO RUNNABLE
STATE AND ANOTHER THREAD GOES TO RUNNING. AM I RIGHT?)


So this conclude and confirm that:

E. The output could be: "Eyra ", followed by an exception.


Recap from K & B book page 766.

Besides those three, we also have the following scenarios in which a thread might
leave the running state:

¦ The thread's run() method completes. Duh.
(OK, THAT MEANS THAT t1 WILL PRINT "Andi ")

¦ A call to wait() on an object (we don't call wait() on a thread, as we'll
see in a moment).
(OK, BUT IN THIS CODE THERE ARE NOT CALLS TO wait() ON ANY OBJECT)

¦ A thread can't acquire the lock on the object whose method code it's
attempting to run.
(OK, BUT THIS IS NOT POSSIBLE IN THIS CODE)

¦ The thread scheduler can decide to move the current thread from running
to runnable in order to give another thread a chance to run. No reason is
needed—the thread scheduler can trade threads in and out whenever it likes.
(OK, BUT ALL ALIVE THREADS WILL GET CHANCES TO RUN TO COMPLETION
AM I RIGHT?)


In summary, the thread scheduler is the controller that governs the execution
of threads in and out whenever it decided to. (I AGREE)

If you run the code above on different machine it will behaves and prints out results
differently. There is nothing in order to which thread will execute first, it depends on
the VM Thread Scheduler to chooses which thread to run. (I AGREE)




*/

Please, if you have time, could you clarify me these points?

Once again, thank you very much for your time. I have tried to explain myself as clear as I can. I am sorry if I did not write something incorrect, but English is not my tongue language