Re: Bug in synchronized() ?

rumi <rumi_smieci-h7QdYz1kt/[email protected]>
Newsgroups gmane.comp.hardware.microcontrollers.tini
Message-ID <[email protected]>
Bryan Armstrong wrote:

> rumi wrote:
>
>> I'm using TINOOS 1.12 but problem still exists :(
>> any ideas ?
>
>
> Please send me a short example that reproduces this problem.
>
//*************************
// source example
//*************************
package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.dalsemi.tininet.http.HTTPServer;
import com.dalsemi.tininet.http.HTTPServerException;

/**
 * @author rumcajs
 */
public class SynTest {
    public static final String WEB_ROOT = "/";
    public static final String WEB_INDEX = "index.html";
    public String webLog = WEB_ROOT + "/web.log";
    private Object lock = new Object();
    static boolean debugOn = false;
    private File file;
    private int index;

    public SynTest() {
        this.file = new File(WEB_ROOT + WEB_INDEX);

        new Thread() {
            public void run() {
                try {
                    while (true) {
                        System.out.println("writing file");
                        synchronized (lock) {
                            prepareFile();
                        }
                        System.out.println("write finished");
                        Thread.sleep(1000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
        .start();
        // create an instance of HTTPServer on port httpPort
        HTTPServer httpServer = new HTTPServer(80, debugOn);
        // override the default index page
        httpServer.setIndexPage(WEB_INDEX);
        // override the default HTTP root
        httpServer.setHTTPRoot(WEB_ROOT);
        // override the default log file name
        httpServer.setLogFilename(webLog);
        try {
            httpServer.setLogging(debugOn);
        } catch (HTTPServerException h) {
            h.printStackTrace();
        }

        while (true) {
            try {
                // Threaded web server blocks on accept
                httpServer.serviceRequests(lock);
            } catch (HTTPServerException h) {
                if (debugOn) {
                    System.out.println(h.toString());
                }
            } catch (Throwable t) {
                if (debugOn) {
                    // why kill the server if the exception is not fatal?
                    System.out.println(t.toString());
                }
            }

        }
    }
    public static void main(String[] args) {
        new SynTest();
    }
    private void prepareFile() throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        for(int i=0;i<100;i++){
            fos.write(("Some text written to file:" + 
(index++)+"\n").getBytes());
        }
        fos.close();
    }

}
//**************************************
// source end
//**************************************


I suspect  that writing to file causes leaving monitor and web server 
can serve pieces of file. But it should be impossible :), especially 
when web server serves different file than just written it is waiting 
for leaving synchronized block.   - just opposite to my requirements

How to block http requests on file I'm woring on and allow others(for 
example small unchangeable images).

Thank you for your help.
rumi
_______________________________________________
TINI mailing list
TINI-6tN4nzCoH/[email protected]
To UNSUBSCRIBE, edit your profile, or see list archives:
http://lists.dalsemi.com/mailman/listinfo/tini
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.