Link to home
Start Free TrialLog in
Avatar of sraveend
sraveend

asked on

java programming

public class FileSync {
  private File outFile;
  private PrintStream outStream;

  public FileSync() throws IOException {
      outFile = new File("c:/orisree/java/ran.txt");
      outStream = new PrintStream(new FileOutputStream(outFile));
  }

  public Object getSyncObject() {
      return outFile;
  }

  public void writeToFile(String text) throws IOException {
      outStream.println(text);
  }
}
**************************************************
public class TestSynch extends Thread {

  public TestSynch() {
  }
  public static void main(String[] args) {
      WriteThread[] threads = new WriteThread[3];
      try {
          for(int i = 0; i < threads.length; i++) {
              threads[i] = new WriteThread();
          }

          for(int i = 0; i < threads.length; i++) {
              threads[i].start();
          }

      }catch(IOException ioe) {
          ioe.printStackTrace(System.err);
      }
  }
}
***************************
public class WriteThread extends Thread {
  private static int threadCount;
  private int threadNum;
  private FileSync fileSync;

  static {
      threadCount = 0;
  }

  public WriteThread() throws IOException {
      threadNum = ++threadCount;
      fileSync = new FileSync();
  }

  public void run() {
      Object syncObject = fileSync.getSyncObject();
      synchronized(syncObject) {
          for(int i = 0; i < 100; i++) {
              try {
                  fileSync.writeToFile("Printing from thread " + threadNum + " line " + i);
              }catch(IOException ioe) {
                  ioe.printStackTrace(System.err);
              }
          }
      }
  }
}



From the above 3 classes,when i run TestSynch i get output , for 1 to 21, i get threadnum=1
then from 22 to 54, i get threadnum=3 ,after that until 99 i get threadnum=1,
How does synchObject allow thread 3 to come in between thread 1.pls give me the answer at the earliest.
Urgent..


Avatar of Mick Barry
Mick Barry
Flag of Australia image

>       synchronized(syncObject) {

the synchronized code block can only be run by one thread at a time.
other will be blocked until the thread completes the block.
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sraveend
sraveend

ASKER

U r correct only one thread runs,then how i got the thread 3 in between thread1 in my program
thats just how the threads get scheduled, try running it a few times and see.
Basically, every thread takes some time on the CPU. Then, after its time is taken the next thread gets its chance. So, from 1-21 was threads 1 time running on the CPU. Then, you got thread 3  to run on the CPU. And so on, the scheduling is fully random.

Is that what you are looking for, or do you want to know how to block Thread 1 to execute?

AJ
> And so on, the scheduling is fully random.

not sure that its exactly random
It's not random. Higher priority threads get more processor time than lower priority threads. Other than that, the exact behavior (thread selection) is dependent on the VM and the operating system.

A running thread remains running until it blocks, yields or is forcibly swapped off the processor by the thread scheduler.
yes that sounds better, the link above provides some more details.
I just don't find any difference in the output with Synchronization code or without synchronization code.
Is it due to ,blocking only the file and not the printstream object.I just want to know the role of synchronization......
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> .I just want to know the role of synchronization......

It is to ensure only one threads executes a block of code at the same time.
Hello,
I think this might just help u
try setting the Priority of the threads set the maximum priority which is to be completed first

Thread.MAX_PRIORITY
Thread.MIN_PRIORITY
Thread.NORM_PRIORITY