Saturday, 28 March 2009
Sunday, 22 March 2009
Sunday, 1 March 2009
Monday, 9 February 2009
Advanced Thread Programming: Synchronization
import java.util.*;
public class Que {
/**
* @param args
*/
public static void main(String[] args) {
mapQ q = new mapQ();
new genQ(q);
new conQ(q);
}
}
////////////////// the commodity Queue /////////////////
class mapQ {
String s;
LinkedList que = new LinkedList();
synchronized void put(String s) {
if (que.size()>10)
try {
wait();
}
catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
que.addLast(s);
System.out.println("Put: " + s);
notify();
}
synchronized String get() {
if (que.isEmpty())
try {
wait();
}
catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
String temps = (String) que.removeFirst();
//System.out.println(que);
System.out.println("Got: " + temps);
notify();
return temps;
}
}
//////////////// producer /////////////////////
class genQ implements Runnable {
mapQ q;
genQ(mapQ q) {
this.q=q;
new Thread(this, "Producer").start();
}
public void run() {
int i=0;
while(true) {
int tempi = i++;
q.put(Integer.toString(tempi));
}
}
}
/////////////// consumer ///////////////////////
class conQ implements Runnable {
mapQ q;
conQ(mapQ q) {
this.q=q;
new Thread(this, "Consumer").start();
}
public void run() {
while(true) {
q.get();
}
}
}
public class Que {
/**
* @param args
*/
public static void main(String[] args) {
mapQ q = new mapQ();
new genQ(q);
new conQ(q);
}
}
////////////////// the commodity Queue /////////////////
class mapQ {
String s;
LinkedList que = new LinkedList();
synchronized void put(String s) {
if (que.size()>10)
try {
wait();
}
catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
que.addLast(s);
System.out.println("Put: " + s);
notify();
}
synchronized String get() {
if (que.isEmpty())
try {
wait();
}
catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
String temps = (String) que.removeFirst();
//System.out.println(que);
System.out.println("Got: " + temps);
notify();
return temps;
}
}
//////////////// producer /////////////////////
class genQ implements Runnable {
mapQ q;
genQ(mapQ q) {
this.q=q;
new Thread(this, "Producer").start();
}
public void run() {
int i=0;
while(true) {
int tempi = i++;
q.put(Integer.toString(tempi));
}
}
}
/////////////// consumer ///////////////////////
class conQ implements Runnable {
mapQ q;
conQ(mapQ q) {
this.q=q;
new Thread(this, "Consumer").start();
}
public void run() {
while(true) {
q.get();
}
}
}
Labels:
java
Sunday, 1 February 2009
Thursday, 29 January 2009
Jython and Matlab do jobs for java
Java codes:
======================
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyExample
implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Ouch!");
}
public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
frame.setSize(300,300);
JButton button = new JButton("Push Me!");
ActionListener listener = new MyExample();
button.addActionListener(listener);
frame.getContentPane().add(button);
frame.setVisible(true);
}
}
Jython:
======================
import javax.swing as swing
def printMessage(event):
print "Ouch!"
frame=swing.JFrame("my frame")
frame.setSize(300,300)
button=swing.JButton("Push Me!")
button.actionPerformed=printMessage
frame.getContentPane().add(button)
frame.setVisible(True) #frame.visible=1
Matlab:
=======================
import javax.*;
frame = swing.JFrame('my frame');
frame.setSize(300,300);
button = swing.JButton('Push Me!');
set(button,'ActionPerformedCallback',@printMessage);
frame.getContentPane.add(button);
frame.setVisible(true);
%code for sub function:
function printMessage(handle,event)
disp('ouch');
end
======================
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyExample
implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Ouch!");
}
public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
frame.setSize(300,300);
JButton button = new JButton("Push Me!");
ActionListener listener = new MyExample();
button.addActionListener(listener);
frame.getContentPane().add(button);
frame.setVisible(true);
}
}
Jython:
======================
import javax.swing as swing
def printMessage(event):
print "Ouch!"
frame=swing.JFrame("my frame")
frame.setSize(300,300)
button=swing.JButton("Push Me!")
button.actionPerformed=printMessage
frame.getContentPane().add(button)
frame.setVisible(True) #frame.visible=1
Matlab:
=======================
import javax.*;
frame = swing.JFrame('my frame');
frame.setSize(300,300);
button = swing.JButton('Push Me!');
set(button,'ActionPerformedCallback',@printMessage);
frame.getContentPane.add(button);
frame.setVisible(true);
%code for sub function:
function printMessage(handle,event)
disp('ouch');
end
Labels:
java
Subscribe to:
Posts (Atom)