• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

khalid question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Questions from khalid, I am not sure of the answere can somebody
explain to me
1.Given that a static method doIt() in a class Work represents work to be done, what block of code will succeed in starting a new thread that will do the work?
CODE BLOCK A:
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
Thread t = new Thread(r);
t.start();
CODE BLOCK B:
Thread t = new Thread() {
public void start() {
Work.doIt();
}
};
t.start();
CODE BLOCK C:
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
r.start();
CODE BLOCK D:
Thread t = new Thread(new Work());
t.start();
CODE BLOCK E:
Runnable t = new Runnable() {
public void run() {
Work.doIt();
}
};
t.run();
////
2.Which method implementations will write the given string to a file named "file", using UTF8 encoding?
IMPLEMENTATION A:
public void write(String msg) throws IOException {
FileWriter fw = new FileWriter(new File("file"));
fw.write(msg);
fw.close();
}
IMPLEMENTATION B:
public void write(String msg) throws IOException {
OutputStreamWriter osw =
new OutputStreamWriter(new FileOutputStream("file"), "UTF8");
osw.write(msg);
osw.close();
}
IMPLEMENTATION C:
public void write(String msg) throws IOException {
FileWriter fw = new FileWriter(new File("file"));
fw.setEncoding("UTF8");
fw.write(msg);
fw.close();
}
IMPLEMENTATION D:
public void write(String msg) throws IOException {
FilterWriter fw = FilterWriter(new FileWriter("file"), "UTF8");
fw.write(msg);
fw.close();
}
IMPLEMENTATION E:
public void write(String msg) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(
new OutputStream(new File("file")), "UTF8"
);
osw.write(msg);
osw.close();
}

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Satheesh,
Well, I'll take a shot at the first question anyway.
I think code block A is your best bet here. Let's look at block A real quick and then I'll explain why.

First, in a thread class you have a method call run(). The way threads work you should instantiate your thread and then call the start() method on the thread object you just created. The call to start registers the thread and then calls the run() method itself.
The way to make a thread is to extend the thread class and defined the run method in there or you can create a runnable and pass it to the thread at instantiation.
So, in block A you can see the second method in action. In lines 1-5 you see the creation of the runnable. And then in line 6 you see see that created object being passed to the constructor as a new thread is created. Then, as we discussed earlier you see the start() method being called which registers the thread and calls the runnable you passed in to the constructor.
All the other blocks(someone check me on this!) have little issues that make them not correct.
Block B looks okay until you realize the thread they are defining is defining a start() method and not a run() method like it should so that won't work right. There will be problems here registering as a thread and implicitly calling the run method.
Block C defines the runnable okay, but never passes the runnable to a thread and hence never starts a thread to get the desired result.
Block D won't work, just because a new instance of the Work class is created doesn't mean the static doIt() method will be called, not to mention that unless Work implements Runnable passing this into the thread could be a problem.
Block E is close, but no go. We make a good runnable and pass it in and we're all fine! But oh, oops we call run instead of start() so this isn't registered with the JVM as a thread that will be eligible for "time slices".
What's everyone else think?
Hope this helps
[This message has been edited by Jason Stortz (edited January 15, 2001).]
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2. b - OutputSringWriter and InputStreamReader are the only ones where you can specify an encoding scheme.
 
The only thing that kept the leeches off of me was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic