File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Flow" Watch "Thread Flow" New topic
Author

Thread Flow

vineet kumar chaudhry
Ranch Hand

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

[code]

package clientserver;

import java.net.*;
import java.util.Scanner;
import java.io.*;

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 ));
th.start();
System.out.println("////////////Thread State ----> " + th.getState()+"////////////////");
}


} catch (Exception e) {
e.printStackTrace();
}
}

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

class ThreadExample extends Thread
{

public ThreadExample(Runnable r){
super(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 ="";

try{

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

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

while (bis.available()>0){
char ch = (char)bis.read();
//System.out.println(ch);
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
//.getAbsolutePath();

//File historyXmlFile = new File(outputdirectoryabsolutepath);

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

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

fileoutputstream.write(tempBytes);
// System.out.println("Written the data");
fileoutputstream.close();

}


FileInputStream fin = new FileInputStream(xmlFile);

int length = fin.available();
System.out.println(length);
System.out.println(length);

fin.close();

Scanner scanner = new Scanner(xmlFile).useDelimiter("</history>");
//System.out.println("Got scanner");
String line = scanner.next();
//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);

fileoutputstream.write(b);
fileoutputstream.close();
System.out.println("Completed");

//bos.write(by);
//bos.close();
}

catch (Exception e){
e.printStackTrace();
}

}
}
[code]


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.


S.D. MADHAN
Not many get the right opportunity !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Flow