我正在使用同步锁定器来锁定两个数组列表:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/* This class represents data base model to
* be accessed via clients
*/
public class DataBase {
// Scheduled shows
private List<Show> shows;
// Rejected shows
private List<Show> rejectedShows;
//Complete this variable: Can be changed
private static int numberOfShowsToSchedule;
// Singleton
private static DataBase _db;
// Singleton method
public static DataBase getDatabase() {
if (_db == null)
_db = new DataBase();
return _db;
}
// Class constructor
private DataBase() {
synchronized(this){
//synchronized(shows){
this.shows = new ArrayList<Show>();
//}
this.numberOfShowsToSchedule = Constants.maxNumberOfShows;
//synchronized(rejectedShows){
this.rejectedShows = new ArrayList<Show>();
//}
}
}
// Add show
public synchronized boolean addShow(Show show) throws Exception {
synchronized (shows) {// the show LOCKER
if (null == show)
return false;
if (numberOfShowsToSchedule <= 0)// there is a place in the schedule
return false;
for (Show s : shows)
// check if the time is show(name) or time is taken.
if ((show.getStartTime() == s.getStartTime())||(show.getName() == s.getName()))
return false;
numberOfShowsToSchedule--;
Boolean added;
added=shows.add(show);
System.out.println(added+" ** "+show.toString()
+"\n"+shows.size()+"# SHOWS: "+shows.toString());
return added;
//return shows.add(show);
}
}
// Add rejected show
public synchronized boolean addRejectedShow(Show show) {
synchronized (rejectedShows) {// the rejected shows LOCKER
if (null == show)
return false;
return rejectedShows.add(show);
}
}
// Get the number of shows to schedule
public synchronized int getNumberOfShowsToSchedule() {
return numberOfShowsToSchedule;
}
public List<Show> GetShows() {//only used for info
return shows;
}
public List<Show> GetRejectedShows() {//only used for info
return rejectedShows;
}
}
和线程:
import java.util.Random;
public class ViewClient extends Thread {
public int ShowNmuber;
public void run() {//The Manager(Thread)
Random rn = new Random();
Show tempshow = new Show((rn.nextInt(13) + 8),
Constants.existingShows[ShowNmuber]);
System.out.println("->Show Created " + tempshow);
try {
if(Controller.getController().addShow(tempshow))
return;
while(Controller.getController().getNumberOfShowsToSchedule()>0){
tempshow.setStartTime((rn.nextInt(13) + 8));
if(Controller.getController().addShow(tempshow))
return;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void SetShowNumber(int ShowNumber){
this.ShowNmuber=ShowNumber;
//System.out.println("---------------------"+ShowNumber);
}
}
输出:
THREAD NO.1Satrted,
THREAD NO.2Satrted,
THREAD NO.3Satrted,
THREAD NO.4Satrted,
THREAD NO.5Satrted,
THREAD NO.6Satrted,
THREAD NO.7Satrted,
THREAD NO.8Satrted,
THREAD NO.9Satrted,
THREAD NO.10Satrted,
THREAD NO.11Satrted,
THREAD NO.12Satrted,
THREAD NO.13Satrted,
THREAD NO.14Satrted,
THREAD NO.15Satrted,
THREAD NO.16Satrted,
->Show Created {The americans||20:00}
->Show Created {Arrow||20:00}
->Show Created {Person of interest||18:00}
->Show Created {Criminal minds||19:00}
->Show Created {Revenge||8:00}
->Show Created {Family guy||19:00}
->Show Created {Aria||20:00}
->Show Created {The flash||20:00}
->Show Created {Homeland||8:00}
->Show Created {The following||8:00}
->Show Created {Gotham||18:00}
->Show Created {Agnents of shield||8:00}
->Show Created {American idol||15:00}
->Show Created {Game of thrones||15:00}
->Show Created {Black sails||9:00}
->Show Created {My Friend||15:00}
true ** {Game of thrones||15:00}
1# SHOWS: [{Game of thrones||15:00}]
true ** {Aria||20:00}
1# SHOWS: [{Aria||20:00}]
true ** {Person of interest||18:00}
1# SHOWS: [{Person of interest||18:00}]
true ** {Arrow||20:00}
1# SHOWS: [{Arrow||20:00}]
true ** {Black sails||9:00}
2# SHOWS: [{Person of interest||18:00}, {Black sails||9:00}]
true ** {Family guy||19:00}
2# SHOWS: [{Arrow||20:00}, {Family guy||19:00}]
true ** {Criminal minds||8:00}
3# SHOWS: [{Person of interest||18:00}, {Black sails||9:00}, {Criminal minds||8:00}]
true ** {American idol||15:00}
4# SHOWS: [{Person of interest||18:00}, {Black sails||9:00}, {Criminal minds||8:00}, {American idol||15:00}]
true ** {Agnents of shield||16:00}
5# SHOWS: [{Person of interest||18:00}, {Black sails||9:00}, {Criminal minds||8:00}, {American idol||15:00}, {Agnents of shield||16:00}]
true ** {Gotham||12:00}
6# SHOWS: [{Person of interest||18:00}, {Black sails||9:00}, {Criminal minds||8:00}, {American idol||15:00}, {Agnents of shield||16:00}, {Gotham||12:00}]
true ** {The following||19:00}
7# SHOWS: [{Person of interest||18:00}, {Black sails||9:00}, {Criminal minds||8:00}, {American idol||15:00}, {Agnents of shield||16:00}, {Gotham||12:00}, {The following||19:00}]
true ** {The americans||20:00}
8# SHOWS: [{Person of interest||18:00}, {Black sails||9:00}, {Criminal minds||8:00}, {American idol||15:00}, {Agnents of shield||16:00}, {Gotham||12:00}, {The following||19:00}, {The americans||20:00}]
==========================================================================
8# SHOWS: [{Person of interest||18:00}, {Black sails||9:00}, {Criminal minds||8:00}, {American idol||15:00}, {Agnents of shield||16:00}, {Gotham||12:00}, {The following||19:00}, {The americans||20:00}]
REJECTED SHOWS: []
TIME LEFT TO FILL: 0
==========================================================================
**正如您在输出值中看到的那样被替换为另一个,所以我想也许是因为同步不起作用并锁定了ArrayList