This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Thread Flow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Flow" Watch "Thread Flow" New topic

Thread Flow

vineet kumar chaudhry
Ranch Hand

Joined: Apr 02, 2011
Posts: 45
Hi Java Loverz


package clientserver;

import java.util.Scanner;

public class Server{

byte[] data;
Socket socket;
private int ID;
ServerSocket serverSocket;
int in;
int count = 0;
third ob = new third();
public Server() {
try {

serverSocket = new ServerSocket(7777);

System.out.println("i am server & listening...");
while (true) {

socket = serverSocket.accept();

ThreadExample th = new ThreadExample(new NewThread(ob , socket ));
System.out.println("////////////Thread State ----> " + th.getState()+"////////////////");

} catch (Exception e) {

public static void main(String[] args) {
new Server();

class ThreadExample extends Thread

public ThreadExample(Runnable r){

class NewThread implements Runnable
private third ob1;
private Socket socket;

NewThread(third ob,Socket socket)
this.socket = socket;
this.ob1 = ob;

public void run()
System.out.println("Thraead name --->" + Thread.currentThread().getName());

ob1.receiveFile( socket );



class third
synchronized public void receiveFile(Socket socket){

BufferedInputStream bis;
BufferedOutputStream bos;
String str ="";


System.out.println("a client connect");
System.out.println("------1--------" +socket.isClosed());

bis = new BufferedInputStream(socket.getInputStream());

while (bis.available()>0){
char ch = (char);
str = str + ch;

//System.out.println("Data from client --->" + str);
System.out.println("???Thraed State --->" + Thread.currentThread().getState());
System.out.println("???Thread Name --->" + Thread.currentThread().getName());

File xmlFile = new File("C:\\Data.xml");
if (!xmlFile.exists()) {

//String outputdirectoryabsolutepath = outputDirectory

//File historyXmlFile = new File(outputdirectoryabsolutepath);

// System.out.println("Created File");
FileOutputStream fileoutputstream = new FileOutputStream(
// System.out.println("Opened STream");
String temp = "<history>\n</history>";

byte tempBytes[] = temp.getBytes();
// System.out.println("Got byte Data");

// System.out.println("Written the data");


FileInputStream fin = new FileInputStream(xmlFile);

int length = fin.available();


Scanner scanner = new Scanner(xmlFile).useDelimiter("</history>");
//System.out.println("Got scanner");
String line =;
//System.out.println("Line --->" + line);
// System.out.println("Got the line object");
//System.out.println("DATA --->"+ str);
//System.out.println("line.substring(0,(length-11)) --->" + line.substring(0,(length-11)));
String newLine = line.substring(0, (length - 11))
+ str;
// System.out.println(newLine);

byte b[] = newLine.getBytes();

FileOutputStream fileoutputstream = new FileOutputStream(xmlFile);



catch (Exception e){


I wanna know

When first thread holds the lock on synchronized method ,wht does the other thread do

1>Does it enters the run mehod or not

I need to verify that the second thread is waiting for its chance while another is working
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

I believe, when multiple threads are involve, all of them will enter the run method and proceed upto the call to the synchronized receiveFile method. However, the first thread to enter the receiveFile method will block the rest of the threads until completion.

Not many get the right opportunity !
I agree. Here's the link:
subject: Thread Flow
jQuery in Action, 3rd edition