Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Java thread lock example
var net = require("net")
, fs = require("fs")
, filename = "messages.txt";
var server = net.createServer(function(socket) {
console.log("== Client connected");
socket.on("data", function(data) {
var message = data.toString();
console.log("== Client sent:", message);
fs.createWriteStream(filename, { flags: "a" }).write(message);
fs.readFile(filename, function(err, data) {
socket.write(message);
socket.end();
});
});
socket.on("end", function() {
console.log("== Client disconnected");
});
}).listen(9876);
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package threadlock;
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 ThreadLock extends Thread {
private int currentThread;
public ThreadLock(int i) {
this.currentThread = i;
}
@Override
public void run() {
for(int i = this.currentThread; i < this.currentThread + 3; i++) {
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! I'm a client " + i + "!\n");
System.out.println(dis.readUTF());
} catch (UnknownHostException ex) {
Logger.getLogger(ThreadLock.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ThreadLock.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* @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) {
ThreadLock tl = new ThreadLock(i);
tl.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.