001/****************************************************************************
002 * Copyright/Copyleft:
003 *
004 * For this source the LGPL Lesser General Public License,
005 * published by the Free Software Foundation is valid.
006 * It means:
007 * 1) You can use this source without any restriction for any desired purpose.
008 * 2) You can redistribute copies of this source to everybody.
009 * 3) Every user of this source, also the user of redistribute copies
010 *    with or without payment, must accept this license for further using.
011 * 4) But the LPGL is not appropriate for a whole software product,
012 *    if this source is only a part of them. It means, the user
013 *    must publish this part of source,
014 *    but don't need to publish the whole source of the own product.
015 * 5) You can study and modify (improve) this source
016 *    for own using or for redistribution, but you have to license the
017 *    modified sources likewise under this LGPL Lesser General Public License.
018 *    You mustn't delete this Copyright/Copyleft inscription in this source file.
019 *
020 * @author Hartmut Schorrig: hartmut.schorrig@vishia.de, www.vishia.org
021 * @version 0.93 2011-01-05  (year-month-day)
022 *******************************************************************************/ 
023package org.vishia.java2C.test;
024
025import java.util.LinkedList;
026
027
028/**This class demonstrates and tests the usage of some container concepts. 
029 *
030 */
031public class TestContainer 
032{
033
034        private final LinkedList<Object> linkedList = new LinkedList<Object>();
035        
036        private final SimpleClass anObject = new SimpleClass();
037        
038        void addToList(Object src)
039        {
040                linkedList.add(src);
041        }
042        
043        Object removeFirstfromLinkedList()
044        {
045                if(linkedList.size() >0){
046                        Object data = linkedList.remove();
047                  return data;
048                } else {  
049                  return null;
050                }
051        }
052        
053        void addSomeData()
054        {
055                for(int ix=0; ix<5; ix++){
056                        SimpleClass data = new SimpleClass();  //alloc in heap
057                        linkedList.add(data);
058                }
059                
060        }
061        
062        
063        void getAndRemoveAllData()
064        { Object data;
065                do{
066                        data = removeFirstfromLinkedList();
067                }while(data != null);
068        }
069        
070        
071        
072        void test()
073        { addSomeData();
074    getAndRemoveAllData();
075          stressTest.start();   //runs the thread, but the method finished.  
076        }
077
078        
079        
080        /**It adds and removes permanently, But the limit of the heap should be regarded.
081         */
082        private final Thread stressTest = new Thread("stressTestC"){
083                @Override public void run(){
084                        boolean bRun = true;
085                        do{
086                                double whatodo = Math.random();
087                          if(whatodo < 0.25){
088                                //add
089                                //SimpleClass data = new SimpleClass();  //alloc in heap
090                                        linkedList.add(anObject);  //now only alloc node.
091                          } else if(whatodo <0.5){
092                                //remove
093                                        if(linkedList.size()>0){
094                                        linkedList.remove();
095                                }
096                          } else if(whatodo <0.625){
097                                //wait thread
098                                try{ wait(10);} catch(InterruptedException exc){ bRun = false;}
099                          } else {
100                                //do nothing, try again.
101                          }
102                        } while(bRun);  
103                }
104        };      
105        
106        
107        
108}