• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multithreading

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I created a FIFO class in which i put action objects which will be executed by a controller (the controller will assign a thread for each action).

my Fifo action is a singleton pattern (one instance exists) which has a collection containing actions. the controller iterate over all actions in this collection to find tha action to be executed(the first action that was put in the collection ). when a I start an example to put an action into FIFO class (collection), the controller which is iterating over all actions doesn't find the action i have put in the FIFO class.

any suggestions??
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post the code?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our "Threads and Synchronization" forum.
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using a tread pool. Cannot comment without looking at the code.
 
tarik elberrak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code of the 4 classes

the controller manage a thread pool, the reportaction contain params for the work to do by thread, reportviewer do the work ans the fifo classe is used to manage actions as fifo


public class ThreadPoolController implements Runnable{


/**
* controller instance
*/
public static ThreadPoolController instance;

/***
* number of threads in the pool
*/
public static int nbThreads = 5;

/**
* workers
*/
private ReportRetriever[] workers;

/**
* states associated with threads (true idf the thread is free, false if it is running)
*/
private boolean[] states;


/**
* object of synchronization
*/
private static Object synchronizedObjet = new Object();


public FIFOActionQueue fifoinstance = null;


/**
* constructor
*/
private ThreadPoolController(int nbTh) {
super();
fifoinstance = FIFOActionQueue.getInstance();
workers = new ReportRetriever[nbTh];
states = new boolean[nbTh];

for( int i=0; i<workers.length; i++){
workers[i] = new ReportRetriever(i);
states[i] = true; //free thread
}


------------------------------------------------------------------------

/**
* FIFOActionQueue.java, Created on 10 mars 2006 at 14:57:47
*
*/
import java.util.ArrayList;

/**
* @author tarik.el-berrak
*
*/
public class FIFOActionQueue{

/**
* contains action elements
*/
public ArrayList contents;

/**
* queue instance
*/
public static FIFOActionQueue instance;

/**
* object of synchronization
*/
private static Object synchronizedObjet = new Object();

/**
* the constructor
*/
private FIFOActionQueue() {
contents = new ArrayList();
}

/**
*
* @return an instance of a queue, sychronized for multithreahding
*/
public static FIFOActionQueue getInstance() {
if (null == instance) {
synchronized(synchronizedObjet){
instance = new FIFOActionQueue();
}
}
return instance;
}

/**
* add a report action to a collection
*/
public synchronized void add(ReportAction a){
contents.add(a);
}

/**
* true if the queue is empty
*/
public boolean isEmpty() {

return contents.isEmpty();
}

public synchronized void remove(ReportAction a) {
contents.remove(a);
}


public int size() {

return contents.size();
}

public synchronized void trim(){
this.contents.trimToSize();
}

/**
* get the head of the collection
* @return
*/
public synchronized ReportAction poll(){

if( this.contents.isEmpty()) return null;
ReportAction ra = (ReportAction)this.contents.get(0);

if( ra != null ) {
this.contents.remove(ra);
this.trim();
}

return ra;
}


}
---------------------------------------------------------------------------

/**
* ReportAction.java, Created on 10 mars 2006 at 14:11:45
*
*/

/**
* @author tarik.el-berrak
*
*/
public class ReportAction {


private int idReport;

private int idDeal;

private String url;

private String jndi;

private String jndiFactory;

private String archivePath;

/**
* the constructor
*/
public ReportAction(int _idr, int _idd, String _url, String _jndi, String _jndiFactory, String _archivePath) {
super();
this.idReport=_idr;
this.idDeal=_idd;
this.url=_url;
this.jndi=_jndi;
this.jndiFactory=_jndiFactory;
this.archivePath=_archivePath;
}

public boolean equals(Object arg0) {
return (((ReportAction)arg0).idDeal == this.idDeal && ((ReportAction)arg0).idReport == this.idReport);
}

public int getIdDeal() {
return idDeal;
}
public void setIdDeal(int idDeal) {
this.idDeal = idDeal;
}
public int getIdReport() {
return idReport;
}
public void setIdReport(int idReport) {
this.idReport = idReport;
}
public String getJndi() {
return jndi;
}
public void setJndi(String jndi) {
this.jndi = jndi;
}
public String getJndiFactory() {
return jndiFactory;
}
public void setJndiFactory(String jndiFactory) {
this.jndiFactory = jndiFactory;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getArchivePath() {
return archivePath;
}
public void setArchivePath(String archivePath) {
this.archivePath = archivePath;
}
}
----------------------------------------------------------------------------

/**
* ReportRetriever.java, Created on 13 mars 2006 at 11:15:04
*
*/

/**
* @author tarik.el-berrak
*
*/
public class ReportRetriever extends Thread {

private int workerID;

private ReportAction workToDo;

private boolean state;

/**
* the constructor
*/
public ReportRetriever(int id) {
this.workerID = id;
}


/**
* perform the work
* @param ra Report Action
* @return
*/
public boolean runWork(ReportAction ra){

System.out.println(ra.toString());
return true;

}

public void run(){
runWork(getWorkToDo());
}

public ReportAction getWorkToDo() {
return workToDo;
}
public void setWorkToDo(ReportAction workToDo) {
this.workToDo = workToDo;
}

public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public int getWorkerID() {
return workerID;
}
public void setWorkerID(int workerID) {
this.workerID = workerID;
}


public static void main(String args[]){

ReportRetriever rr = new ReportRetriever(1);
ReportAction ra = new ReportAction(100,20,"","","","");

rr.setWorkToDo(ra);

}

}
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your getInstance method is not thread safe. Try synchronizing the whole method and not just the part you have synched now. Im not sure if double-checked locking works now or not in Java (It has never worked before). Yet I'm positive singled checked locking, or whatever you may call what you did there, does not work.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double-checked locking works under the new memory model if the variable is volatile, or something like that. It's still worthless. What they're doing, of course, is not double-checked locking (as you said) and is completely unsafe.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic