• Post Reply Bookmark Topic Watch Topic
  • New Topic

Query on Multithreading ??

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to write a multithreading program using syncronized block so that one thread should write odd number and other thread should write even number in a same file and in one line like(1,2,3,4,5,6,7,8,9,10).Can any one please give me some idea how should i start.

Thanks & Regards
Bikash
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the "Threads and Synchronization" forum.
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the threading part, you should do something like:

1. Create 2 threads such that each has a reference to the other
2. Have one thread write the first number, while the other waits on it.
3. When the writing thread has finished, it should notify the waiting thread, and begin waiting itself on the thread it has just notified.
4. The number could be a shared static int, and you could use two instances of the same Thread/Runnable class.

For the I/O part, the only important thing is to ensure your output is flushed (not buffered) after each write. There is one extra element to make this solution work, but I'll let you figure that out for yourself...
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have sloved my problem with my below codes but i want to optimize my codes.I have used four classes for that . Can any one please guide me how i can optimize my codes.Below are my codes:-



Thanks & Regards
Bikash
[ July 22, 2004: Message edited by: Bikash Paul ]
 
Gagan Gulati
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code, you are trying to distinguish between Odd and Even numbers which is increasing the complexity.

Better way could have been to use an integer and increase it by one always. This increase of number and writing to a file can be pushed to a synchronized block/method.

It would make sure that you are writing them one by one.
make the threads sleep for a while.

Basically, create a single class and invoke 2 instances of that class thread and then make them write.
Makes sense ?

-G
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code works fine on my machine.
This is surprising, because it's a linux-machine, and you use:

But there is no 'e:' on my machine.
Many windowses will not have an 'e' too, or a cdrom/dvd at 'e:'.
But you hide this problem with an empty, and super-general 'catch (Exception'.
Give at least an errormessage to System.err.
And use
to write to the local directory. Forward slashes work on windows too, saving you the second character, are portable and are more easy to type.

str[i].toString() is senseless - str[i] is already a String.
But I would in general use 'int' which might be produced:

Then oddNumber and evenNumber will only differ in 'init' - 1 and 2.

Opening and closing the FileOutputStream for every writing is not best practice too.
You could open it once, and close it once.

Even better:
Use System.out instead, and redirect output to a file if needed:

This will make debugging more easy.

Almost according to Fletcher, I don't agree to (4.) - this might hide problems, if only one thread generates the numbers.
[ July 27, 2004: Message edited by: Stefan Wagner ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!