Created
March 14, 2012 00:23
-
-
Save brenoferreira/2032933 to your computer and use it in GitHub Desktop.
Thread lock example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package myservice; | |
/** | |
* | |
* @author vcamaral | |
*/ | |
public class Client extends Thread { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
// TODO code application logic here | |
for(int i = 0; i < 12; i += 3) { | |
ConnectionThread ct = new ConnectionThread(i); | |
ct.start(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package myservice; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.net.Socket; | |
import java.net.UnknownHostException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author vcamaral | |
*/ | |
public class ConnectionThread extends Thread { | |
private int currentThread; | |
public ConnectionThread(int i) { | |
this.currentThread = i; | |
} | |
@Override | |
public void run() { | |
for(int i = this.currentThread; i < this.currentThread + 3; i++) { | |
String str = ""; | |
try { | |
Socket s = new Socket("127.0.0.1", 9876); | |
DataOutputStream dos = new DataOutputStream(s.getOutputStream()); | |
DataInputStream dis = new DataInputStream(s.getInputStream()); | |
dos.writeUTF("Hello from client!\n"); | |
str = dis.readUTF(); | |
System.out.println(str); | |
} catch (UnknownHostException ex) { | |
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); | |
} catch (IOException ex) { | |
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package myservice; | |
import java.io.*; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
/** | |
* | |
* @author vcamaral | |
*/ | |
public class MyService { | |
private LockObject lockObj = new LockObject(); | |
public static String readFile(String fileName) throws FileNotFoundException, IOException { | |
File f = new File(fileName); | |
FileInputStream fis = new FileInputStream(f); | |
int tam = (int) f.length(); | |
byte b[] = new byte[tam]; | |
fis.read(b); | |
String str = new String(b); | |
return str; | |
} | |
public static void writeFile(String data) { | |
try { | |
FileWriter fw = new FileWriter("test.txt", true); | |
fw.write(data); | |
fw.flush(); | |
fw.close(); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
// TODO code application logic here | |
String str = ""; | |
while (true) { | |
try { | |
ServerSocket ss = new ServerSocket(9876); | |
System.out.println("Server running..."); | |
Socket s = ss.accept(); | |
DataOutputStream dos = new DataOutputStream((s.getOutputStream())); | |
DataInputStream dis = new DataInputStream(s.getInputStream()); | |
str = dis.readUTF(); | |
synchronized(lockObj){ | |
writeFile(str); | |
dos.writeUTF(readFile("test.txt")); | |
} | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment