我自定义了一个链表类:public class SortedList { public static Link first=Link.getInstance(); // ref to first item public Link vLink = null; public static Link pointer = null; public Link previous = null; public Link current = null; //private SortedList first=new Link(); private SortedList() { first.next = null; first.OrderId="00000"; first.Pnr="DV21E"; first.PreOrderDate="1970-01-01 12:12:12"; first.PreOrderLimit=""; } private static SortedList instance = null; public static synchronized SortedList getInstance() { if (instance == null) { instance = new SortedList(); //first = new Link(); } return instance; } // public SortedList() // constructor public Link getFirst() { // get value of first return first; } 然后在主线程中初始化该链表,主线程代码为: public class testSortedlist extends Thread { public static boolean vRunId = true; public static SortedList a = SortedList.getInstance();//得到链表实例 public static Link ad, d;//链表结点 private static testSortedlist ts; public testSortedlist(boolean first0) { if(a!=null) {a=null;} a=SortedList.getInstance(); } public testSortedlist() { a=SortedList.getInstance(); } public static synchronized testSortedlist getinstance() { if (ts == null) { ts = new testSortedlist(); } return ts; } public static void main(String[] args) {
System.out.println(" -------new LIST print after sleep"); Thread.currentThread().sleep(6000);
ad = a.first; while (ad != null) { System.out.println(ad.PreOrderDate); ad = ad.next; } System.out.println(" sortedlist address in main...url"+SortedList.getInstance());
} catch (Exception e) { e.printStackTrace(); } }System.out.println(SortedList.getInstance()+" address in testst new"); System.out.println(a + " address in testst new");
}
public SortedList getSortedList() { return a; } 其它线程代码为: public class callerInsert extends Thread { public callerInsert() { } public static void main(String[] args) { callerInsert callerInsert1 = new callerInsert(); } public synchronized boolean insertData(String vdata0) { boolean id=false; Link d=new Link(); testSortedlist dd=new testSortedlist(); d.PreOrderDate=vdata0; System.out.println("SortedList in callerInsert:....url:"+SortedList.getInstance());//取得链表,用servlet调该方法,则链表地址变化,为什么? dd.getSortedList().insert(d); id=true; return id; } }
目前是WEB应用,将来提供EJB调用接口.
谢谢关注!
可以用Session对象传递该链表
public static Link first=Link.getInstance(); // ref to first item
public Link vLink = null;
public static Link pointer = null;
public Link previous = null;
public Link current = null;
//private SortedList first=new Link();
private SortedList() { first.next = null;
first.OrderId="00000";
first.Pnr="DV21E";
first.PreOrderDate="1970-01-01 12:12:12";
first.PreOrderLimit=""; } private static SortedList instance = null;
public static synchronized SortedList getInstance() {
if (instance == null) {
instance = new SortedList();
//first = new Link();
}
return instance;
} // public SortedList()
// constructor
public Link getFirst() { // get value of first
return first;
}
然后在主线程中初始化该链表,主线程代码为:
public class testSortedlist
extends Thread
{
public static boolean vRunId = true;
public static SortedList a = SortedList.getInstance();//得到链表实例
public static Link ad, d;//链表结点
private static testSortedlist ts;
public testSortedlist(boolean first0) {
if(a!=null)
{a=null;}
a=SortedList.getInstance();
}
public testSortedlist() {
a=SortedList.getInstance();
} public static synchronized testSortedlist getinstance() {
if (ts == null) {
ts = new testSortedlist();
}
return ts;
} public static void main(String[] args) {
testSortedlist t= testSortedlist.getinstance();
t.run();
System.out.println("in testst");
} public static synchronized void insertdata(String vd1) {//供其它线程调用以插入新结点 testSortedlist.d = new Link();
testSortedlist.d.PreOrderDate = vd1;
a.insert(d); } public void run() {
{
System.out.println("Beep!");
while (vRunId) {
try {
System.out.println(" -------new LIST print after sleep");
Thread.currentThread().sleep(6000);
ad = a.first;
while (ad != null) {
System.out.println(ad.PreOrderDate);
ad = ad.next;
}
System.out.println(" sortedlist address in main...url"+SortedList.getInstance());
}
catch (Exception e) {
e.printStackTrace();
}
}System.out.println(SortedList.getInstance()+" address in testst new");
System.out.println(a + " address in testst new");
}
public SortedList getSortedList() {
return a;
}
其它线程代码为:
public class callerInsert extends Thread {
public callerInsert() {
}
public static void main(String[] args) {
callerInsert callerInsert1 = new callerInsert();
}
public synchronized boolean insertData(String vdata0)
{
boolean id=false; Link d=new Link();
testSortedlist dd=new testSortedlist();
d.PreOrderDate=vdata0;
System.out.println("SortedList in callerInsert:....url:"+SortedList.getInstance());//取得链表,用servlet调该方法,则链表地址变化,为什么?
dd.getSortedList().insert(d);
id=true;
return id;
}
}