Monday May 19, 2008
Monday May 19, 2008
import java.io.*;
public class ReadString {
public static void main (String[] args) throws Exception {
System.out.print("Enter your name: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
System.out.println("Thanks: " + name);
}
}
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel l = new JLabel("label:");
JTextField f = new JTextField(10);
JButton b = new JButton("Calc");
panel.add(l);
panel.add(f);
panel.add(b);
frame.getContentPane().add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class Test2 {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4,2));
JLabel l1 = new JLabel("label:");
JTextField f1 = new JTextField(10);
panel.add(l1);
panel.add(f1);
JLabel l2 = new JLabel("label:");
JTextField f2 = new JTextField(10);
panel.add(l2);
panel.add(f2);
JLabel l3 = new JLabel("label:");
JTextField f3 = new JTextField(10);
panel.add(l3);
panel.add(f3);
JButton b = new JButton("Calc");
panel.add(b);
frame.getContentPane().add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class Test3 {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4,1));
JLabel l1 = new JLabel("label:");
JTextField f1 = new JTextField(10);
JPanel panel1 = new JPanel();
panel1.add(l1);
panel1.add(f1);
panel.add(panel1);
JLabel l2 = new JLabel("label:");
JTextField f2 = new JTextField(10);
JPanel panel2 = new JPanel();
panel2.add(l2);
panel2.add(f2);
panel.add(panel2);
JLabel l3 = new JLabel("label:");
JTextField f3 = new JTextField(10);
JPanel panel3 = new JPanel();
panel3.add(l3);
panel3.add(f3);
panel.add(panel3);
JButton b = new JButton("Calc");
JPanel panel4 = new JPanel();
panel4.add(b);
panel.add(panel4);
frame.getContentPane().add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
Wednesday Jun 20, 2007
Friday Dec 15, 2006
if (!(wait)) break;
wait();
class CRun1 implements Runnable {
String s;
CountDownLatch c = new CountDownLatch(1);
CRun1(String s) {
this.s = s;
}
public void run() {
System.out.println("Lrun " + s);
try {
Thread.sleep(4000);
} catch (InterruptedException ex) {
} finally {
}
System.out.println("count down - notify for run " + s);
c.countDown();
}
public void get() {
while (true) {
try {
c.await();
break;
} catch (InterruptedException ex) {
}
}
System.out.println("recv notify...");
}
}
class Run1 implements Runnable {
String s;
boolean wait = true;
Run1(String s) {
this.s = s;
}
public synchronized void run() {
System.out.println("run " + s);
try {
Thread.sleep(4000);
} catch (InterruptedException ex) {
}
wait = false;
System.out.println("notify for run " + s);
notifyAll();
}
public synchronized void get() {
while (true) {
try {
System.out.println("wait...");
wait();
if (!(wait)) break;
System.out.println("recv notify...");
} catch (InterruptedException ex) {
}
}
}
}
public class TestThread {
public synchronized void testIt() throws Exception {
Run1 run1 = new Run1("1");
Run1 run2 = new Run1("2");
Thread t1 = new Thread(run1);
t1.start();
Thread t2 = new Thread(run2);
t2.start();
run1.get();
run2.get();
}
public static void main(String[] args) throws Exception {
TestThread t = new TestThread();
t.testIt();
}
}
Wednesday Dec 06, 2006
public interface ComputeServer extends Remote {
public double integrate(String function, double min, double max) throws RemoteException;
}
public class ComputeServerImpl extends UnicastRemoteObject implements ComputeServer {
public double integrate(String function, double min, double max) throws RemoteException {
...
}
}
public double integrate(String function, double min, double max) throws RemoteException {
class FunctionToIntegrate {
String function;
int min;
int max;
}
public double integrate(FunctionToIntegrate function);
class FunctionToIntegrate extends Serializable {
...
}
public class ComputeServerImpl extends UnicastRemoteObject implements ComputeServer {
public void integrate(String function, Remote callback) throws RemoteException {
...
}
}