Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Devaka Cooray
Liutauras Vilda
Sheriffs:
Jeanne Boyarsky
paul wheaton
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Carey Brown
Mikalai Zaikin
Bartenders:
Lou Hamers
Piet Souris
Frits Walraven
Forum:
Programmer Certification (OCPJP)
Question about threads in Kathy's Book.I don't understand.
Daniel Faria
Greenhorn
Posts: 15
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public class Messanger implements Runnable{ publi static void main(String [] args){ new Thread(new Messenger("Wallace")); new Thread(new Messenger("Gromit")); } private String name; public Messenger(String name){ this.name=name } } public void run(){ message(1); message(2) } private syncronized void message(int n){ System.out.println(name+ "-"+n+"") } }
This correct answer in book is Wallace-1 Gromit-1 Gromit-2 Wallace-2, but i don't understand if two
threads have differents instances. How know what showing??
Thanks.
Ulf Dittmer
Rancher
Posts: 43081
77
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
That code will not output anything, because the threads are never started. Are you sure that this is the code the question is about?
Daniel Faria
Greenhorn
Posts: 15
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Sorry, this is wrong.
A correct code is:
public class Messanger implements Runnable{ publi static void main(String [] args){ new Thread(new Messenger("Wallace")).start(); new Thread(new Messenger("Gromit")).start(); } private String name; public Messenger(String name){ this.name=name } } public void run(){ message(1); message(2) } private syncronized void message(int n){ System.out.println(name+ "-"+n+"") } }
I forget put start method.
Thanks.
Punit Singh
Ranch Hand
Posts: 952
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
This code guarantees only one thing that is: Wallace-1 will come before Wallace-2 and Gromit-1 will come before Gromit-2.
Now Wallace and Gromit can come in any order. So you have to pick answer from the order that code is guaranting.
SCJP 6
Daniel Faria
Greenhorn
Posts: 15
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I understand, find this question in enghish version of book.There isn't.
Thanks.
nav katoch
Ranch Hand
Posts: 246
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Br
Java
,
Is Java your last name or just for fun? If not, then please use real name.
Thanks,
Naveen
Jesper de Jong
Java Cowboy
Posts: 16084
88
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
"Br Java", please check your private messages. You can see them by clicking
My Private Messages
.
Jesper's Blog
-
Pluralsight Author Page
The only cure for that is hours of television radiation. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
thread on different instances?
answer of this code
Problem with Chapter 9 Question 1
Threads: synchronized method (K&B 1.5, page 733)
Thread Doubt
More...