Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-02-2012, 08:01 PM   PM User | #1
kainard
New to the CF scene

 
Join Date: Aug 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
kainard is an unknown quantity at this point
Multithreading file writing

Hi all,
I'm looking for a simple java example of a multithread server that creates a number of threads that open and write the same file in a synchronized way.

Can someone link an example or write a sample code? Google didnt help me

Thanks in advance
kainard is offline   Reply With Quote
Old 08-02-2012, 08:38 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Doesn't really matter what you do with it. Simply search for thread synchronization in Java.
All you need to do is control the condition of the wait and notify so that when one thread is writing to the file it blocks all other threads from opening until its finished. When done, notify so that another thread can pick it up.
Fou-Lu is offline   Reply With Quote
Old 08-02-2012, 10:21 PM   PM User | #3
kainard
New to the CF scene

 
Join Date: Aug 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
kainard is an unknown quantity at this point
I found this example http://www.java-samples.com/showtuto...tutorialid=306 it's clear how to use it if I push and pop with different methods, but if I only use a method like this

PHP Code:
    synchronized public void writeStuff(String msgthrows Exception {
       
        
FileOutputStream file = new FileOutputStream("file.txt"true);
        
PrintStream ps = new PrintStream(file);
        
        
ps.println(msg);
        
    } 
in every thread, how do I use wait() and notify()? Or just synchronized is enough?

Last edited by kainard; 08-03-2012 at 01:15 AM..
kainard is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:32 PM.


Advertisement
Log in to turn off these ads.