Gerald Kou

Greenhorn
+ Follow
since Jun 03, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Gerald Kou

General Considerations: Maximum=58 Deductions=0 Actual=58
Documentation: Maximum=20 Deductions=0 Actual=20
GUI: Maximum=24 Deductions=12 Actual=12 Server: Maximum=53 Deductions=0 Actual=53 Total: Maximum=155 Deductions=12
Certification Score=143
Simplicity is pretty good.
20 years ago
After reading most posts about lock() and LockManager,I want to implement the lock mechanism with simplest way.I want only use a HashSet to stroe the locked record number,don't use a HashMap to store record number and clientid. The reason is:
1.I can implements the lock()/unlcok as the signature of requirement need:lock(int record) not LockManager.(int record,Object clientId).
2.As the important key:using HashSet can also fulfill the requirement "The unlock method simply
removes the lock from the specified record. If an attempt is made to unlock a record that has not
been locked by this connection, then no action is be taken" .
when method bookFlight excutes,the sequence is lock, read, modify, write, unlock .So when several clients requent the same record,Only one client can get the record's lock and only the
same client can unlock the record.Meanwhile 0ther client can't get the record's lock and perform
the unlock method.
My means is: the case as below can't occur.
A.lock(21);
...
B.lock(21);
...
A.lock(21);
My choise on lock have little extendable capability.But I think it is exacltly satisfied with the requirement of FBN(Although LockManager is also a good choise).
please point out Whether I will lost mark when using the simple design.Did someone use the
similar design and pass the exam?
OK! Thanks Andrew !
As you kown. Twice Post isn't my mind.but I wait a whole day for the reply.so I post twice to attract attention. I think this case don't occur.
I want to change compnnents of panel in rin time when user click different items of list ,but I can't realize it. I process the event in valueChanged(),I used ervey approach such as repaint(),validate(),invalidate().Nevertheless there is effect by those.
Please see my code,and point out where are the errors.Thanks!
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class test_changeview implements ListSelectionListener {
JFrame topFrame = null;
JList itemList = null;
JLabel statuslabel = null;
JPanel mainPane = null;
JScrollPane infoPane = null;
public test_changeview() {
topFrame = new JFrame("hello!");
topFrame.setBounds(100, 100, 630, 400);
Container contentPane = topFrame.getContentPane();
contentPane.setLayout(new BorderLayout());
JLabel tittlelabel = new JLabel("System", JLabel.CENTER);
statuslabel = new JLabel("hello");
mainPane = new JPanel();
mainPane.setLayout(new BorderLayout());
String[] item = { "item1", "item2" };
itemList = new JList(item);
itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
itemList.setSelectedIndex(0);
itemList.addListSelectionListener(this);
JPanel pane = new JPanel();
pane.add(new JButton("item1"));
infoPane = new JScrollPane(pane);
mainPane.add(itemList, BorderLayout.WEST);
mainPane.add(infoPane, BorderLayout.CENTER);
contentPane.add(tittlelabel, BorderLayout.NORTH);
contentPane.add(mainPane, BorderLayout.CENTER);
contentPane.add(statuslabel, BorderLayout.SOUTH);
topFrame.setVisible(true);
topFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void valueChanged(ListSelectionEvent e) {
System.out.println("ListSelectionEvent: ");
if (e.getValueIsAdjusting())
return;
int index = itemList.getSelectedIndex();
statuslabel.setText("index :" + index);
if (index == 0) {
}
if (index == 1) {
infoPane = new JScrollPane(panelV2());
// infoPane.repaint();
// topFrame.repaint();
// topFrame.validate();
topFrame.invalidate();
}
}
private JPanel panelV2() {
JPanel pane = new JPanel();
pane.add(new JButton("item2"));
return pane;
}
public static void main(String[] args) throws Exception {
new test_changeview();
}
}
please see valueChanged(),and give me a help.
I want to change content in panel when user click different items of list ,but I can't realize it. I process the event in valueChanged(),I used ervey approach such as repaint(),validate(),invalidate().Nevertheless there is effect by those.
Please see my code,and point out where are the errors.Thanks!
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class test_changeview implements ListSelectionListener {
JFrame topFrame = null;
JList itemList = null;
JLabel statuslabel = null;
JPanel mainPane = null;
JScrollPane infoPane = null;
public test_changeview() {
topFrame = new JFrame("hello!");
topFrame.setBounds(100, 100, 630, 400);
Container contentPane = topFrame.getContentPane();
contentPane.setLayout(new BorderLayout());
JLabel tittlelabel = new JLabel("System", JLabel.CENTER);
statuslabel = new JLabel("hello");
mainPane = new JPanel();
mainPane.setLayout(new BorderLayout());
String[] item = { "item1", "item2" };
itemList = new JList(item);
itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
itemList.setSelectedIndex(0);
itemList.addListSelectionListener(this);
JPanel pane = new JPanel();
pane.add(new JButton("item1"));
infoPane = new JScrollPane(pane);
mainPane.add(itemList, BorderLayout.WEST);
mainPane.add(infoPane, BorderLayout.CENTER);
contentPane.add(tittlelabel, BorderLayout.NORTH);
contentPane.add(mainPane, BorderLayout.CENTER);
contentPane.add(statuslabel, BorderLayout.SOUTH);

topFrame.setVisible(true);
topFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void valueChanged(ListSelectionEvent e) {
System.out.println("ListSelectionEvent: ");
if (e.getValueIsAdjusting())
return;
int index = itemList.getSelectedIndex();
statuslabel.setText("index :" + index);
if (index == 0) {

}
if (index == 1) {
infoPane = new JScrollPane(panelV2());
//infoPane.repaint();
//topFrame.repaint();
//topFrame.validate();
topFrame.invalidate();
}
}

private JPanel panelV2() {
JPanel pane = new JPanel();
pane.add(new JButton("item2"));
return pane;
}

public static void main(String[] args) throws Exception {
new test_changeview();
}
}
I make a ID class which has a static couter.when clients request to the server,server get a ID's
couter which is added every once.So all the client can get a unique id.But My question is that if server crashed,then couter is set to zero. I think that some alive client had old clientids which are same to the new id made from the server.
Thank you for the descriptions.I gain it.
I saw some posts about "clientid"'s role.They said that using of clientid can prevent the case as below:
A.lock(27)
..
B.unlock(27)
A.unlock(27)
But I doubted it beacase the sequence of execution is lock-reade-modify-unlock.So if Thread A locked one record suceessfully,then Thread B can't locked the same record.Thread B should blocked in lock() method untill Thread A finish unlock() and notifyAll().
Am I wrong? Please tell me why to use clientid?
ok! I got a lot!
I'll realized lock/unlock as requirement said!
Thanks Andrew !
quote:
---------------------------
I think your question is: why does a single instance of an RMI object use different threads on subsequent calls to RMI methods
---------------------------
It's a exact description of my question!
I had seen " RMI Specification" before. But Now I can't understand the statement "A method dispatched by the RMI runtime to a remote object implementation may or may not execute in a separate thread. " in it.
Why it say "may or may not "? it run in ervery threads or not ?
From my excercise,I find all the client can only gain one single instance of the remote object
which is binded with JNDI name.Because when I change a object's member(means :is not static
member) of the remote object got from one client,I watched that the same member of the remote
object which I got from another client were also changed.
But when I executed the method of the remote object instance, the result is that executions of
the remote object method in different clients are different threads. why occurs this practise?
If the remote object instance is singeton ,why dis result likes many threads(means: many remote
object instances) run.
digist of my code :
public interface IRemote extends Remote {
void prnStr() throws RemoteException;
void setStr(String str) throws RemoteException;
String testSynchronizedStr() throws RemoteException;
}
class IRemoteImpl extends UnicastRemoteObject implements IRemote {
public static int i = 0;
public int j = 0;
String str;
public void prnStr() {
System.out.println("in IRemoteImpl getStr() i= " + i++);
System.out.println("in IRemoteImpl getStr() j= " + j++);
System.out.println("the obj str is " + str);
}
public synchronized String testSynchronizedStr() {
System.out.println("start: testSynchronizedStr");
long count = 0;
while (true) {
if (count % 10000000 == 0)
System.out.println("now count is " + count);
if (count++ > 150000000l)
break;
}
System.out.println("end: testSynchronizedStr");
return "ok";
}
public void setStr(String str) {
this.str = str;
}
}
public class RemoteServer {
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(1099);
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String name = "RemoteTest";
IRemoteImpl oneRemoteObj = new IRemoteImpl();
if (oneRemoteObj == null)
throw new Exception("RemoteFactory error");
Naming.rebind(name, oneRemoteObj);
System.out.println("RemoteTest bound");
} catch (Exception e) {
System.err.println("RemoteTest exception: " + e.getMessage());
e.printStackTrace();
}
}
}
public class Client {
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String jndiName = "RemoteTest";
System.out.println("starting...");
IRemote theRemoteObj1 = (IRemote) Naming.lookup(jndiName);
theRemoteObj1.setStr("obj1");
theRemoteObj1.prnStr();
System.out.println("...........");
IRemote theRemoteObj2 = (IRemote) Naming.lookup(jndiName);
theRemoteObj2.setStr("obj2");
theRemoteObj2.prnStr();
theRemoteObj1.prnStr();

System.out.println("########### test synchronized ############");
System.out.println(theRemoteObj1.testSynchronizedStr());
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception end");
}
}
}
you can start several clients to see the output:
1 the variable "j" is changed by all the clients not by separated clients.
2 testSynchronizedStr() run as in several threads
I can't understand why to use the lock/unlock method in Data.java and why to use lockmanager.
I want every client get a remote singlton Data class and use it to access file.
When the action of booking flight is occured,we can use method book(), as below:
public synchronized void book(..) {
getRecord(..);
...
modify(..);
...
}
As the method getRecord() and modify() is also synchronized methods,I think it is thread safe
when I use a singleton class of Data to access db,db in server side.
what's the lockmanager's role? And why needs clientid? all the clients can get the singleton Data
to access db.If one client blocked by another client's access, it will wait untill notify. So
lockmanager's fuction is not required. It only creates some messages for the client which is
blocked in accessing db ? or it has other profits?
the upper opinions are right? If I'm wrong,please tell me where my thinking was poor! Thanks
I download the project in 2002 Dec.But I began it in May 2003.Now I find that the new assignment have been appeared in Sun's website. I download it too.
which assignment should I do? the older one or the new one?
Help me!My friends!