Results 1 to 17 of 17
  1. #1
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Exclamation extremely hard concurrency error +21

    Hi
    That is my code :

    Java Code:
    class RunnableFactory
    {
    	static Runnable produceRunnable(final CThread cthread)
    	{	
    		return new Runnable()
    		{
    
    			@Override
    			public void run() {
    				
    				try {
    					CThread ct = cthread;
    					ct.start();
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		};
    		
    		
    		
    	}
    }
    
    class Bomb implements Runnable
    {
    	
    	private CThread citizen;
    	
    	public Bomb(CThread citizen)
    	{
    		this.citizen = citizen;
    	}
    
    	@Override
    	public void run() {
    		citizen.terminate();
    	}
    	
    }
    
    public class TimeoutExecutor {
    
    	ThreadPoolExecutor threadService = null;
    	
    	Integer timeout = null;
    	
    	Integer poolSize = null;
    	
    	public TimeoutExecutor(Integer timeout , Integer poolSize)
    	{
    		this.timeout = timeout;
    		this.poolSize = poolSize;
    		threadService = (ThreadPoolExecutor) Executors.newScheduledThreadPool(poolSize/*, new CThreadFactory()*/);
    		threadService.setMaximumPoolSize(poolSize);
    		threadService.setCorePoolSize(poolSize);
    		
    	}
    	public void execute(ArrayList<CThreadMock> cThreadList)
    	{
    
    		assert(threadService != null);
    		for(CThread cthread: cThreadList)
    		{
    			threadService.execute(
    					RunnableFactory.produceRunnable(cthread)
    					);
    			((ScheduledExecutorService) threadService).schedule(new Bomb(cthread),timeout,TimeUnit.SECONDS);
    		}
    		threadService.shutdown();
    		try {
    			threadService.awaitTermination(cThreadList.size()*timeout, TimeUnit.SECONDS);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    }
    and more :

    Java Code:
    public class CThreadMock extends CThread {
    
    	public CThreadMock(Integer integer) {
    		super(integer);
    		// TODO Auto-generated constructor stub
    	}
    
    }
    
    class CThread{
    	
    	Integer some_numb = null;
    	
    	public CThread(Integer integer) {
    		
    		this.some_numb = integer;
    		_thread = new Thread()
    		{
    			@Override
    			public void run()
    			{
    				while(!isInterrupted())
    				{
    					try {
    						Thread.sleep(500);
    					} catch (InterruptedException e) {
    						e.printStackTrace();
    						this.interrupt();
    					}
    					System.out.println(some_numb);
    				}
    			}
    		};
    		
    		_thread.setPriority(some_numb);
    		
    	}
    
    	/** контролируемая нить */
    	private Thread _thread = null;
    	
    	/** метод: запустить нить */
    	public void start() throws Exception {
    		_thread.start();	
    	}
    	
    	/** метод: завершить работу нити */
    	public void terminate() {
    		
    		try {
    			System.out.println(some_numb + " dying");
    			_thread.interrupt();
    		
    		} catch ( Exception ex ) { 
    			
    		} catch ( Throwable ex ) {
    			
    		}
    		
    		
    	}
    	
    	
    }
    and finally main class :

    Java Code:
    		ArrayList<CThreadMock> CThreadList = new ArrayList<CThreadMock>();
    		CThreadList.add(new CThreadMock(1));
    		CThreadList.add(new CThreadMock(2));
    		CThreadList.add(new CThreadMock(3));
    		CThreadList.add(new CThreadMock(4));
    		CThreadList.add(new CThreadMock(5));
    		
    		TimeoutExecutor te = new TimeoutExecutor(10,1);
    		te.execute(CThreadList);
    My goal is to put all of 5 threads to the pool, and pool must run them 1 by 1 with 10 sec. timeout , but somehow all of threads run together. Help me please!

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: extremely hard concurrency error +21

    if they have to run sequentially, why use multiple threads at all? :/ You might as well just use one thread which can execute a list of jobs without all this complex machinery that you built. In fact I think that standard java.util.Timer does that.

    Or perhaps I misunderstand you and you want each following thread to start 10 seconds after the last one?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: extremely hard concurrency error +21

    Quote Originally Posted by gimbal2 View Post
    if they have to run sequentially, why use multiple threads at all? :/ You might as well just use one thread which can execute a list of jobs without all this complex machinery that you built. In fact I think that standard java.util.Timer does that.

    Or perhaps I misunderstand you and you want each following thread to start 10 seconds after the last one?
    What i need is that run only one thread(at a time), after 10 seconds elapsed thread must die, and the second thread must take its place. After 10 seconds elapsed thread must die and so on. Object Bomb blows up the associated thread after 10 seconds elapsed so the next thread takes its place

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: extremely hard concurrency error +21

    What is the intention of this thread with a 10 second timeout? What will it actually DO within those 10 seconds? All I see now in your code is threads that sleeps until it is interrupted; I am assuming that is placeholder code to mock a thread doing "something" for 10 seconds.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: extremely hard concurrency error +21

    Quote Originally Posted by gimbal2 View Post
    What is the intention of this thread with a 10 second timeout? What will it actually DO within those 10 seconds? All I see now in your code is threads that sleeps until it is interrupted; I am assuming that is placeholder code to mock a thread doing "something" for 10 seconds.
    threads intention to print number every 0.5 second if it not interrupted.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: extremely hard concurrency error +21

    And is there a specific reason why you want to use this complex pooling mechanism, in stead of say making the thread itself smart enough to run for only 10 seconds, then ending and starting up the next thread? So a sort of chaining solution in stead of pooling?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: extremely hard concurrency error +21

    Quote Originally Posted by gimbal2 View Post
    And is there a specific reason why you want to use this complex pooling mechanism, in stead of say making the thread itself smart enough to run for only 10 seconds, then ending and starting up the next thread? So a sort of chaining solution in stead of pooling?
    Ok. I need threadpool that terminates every thread after N seconds elapsed after this thread was started by threadpool. I found this article helpful but example from stackoverflow doesnot work for me(
    I only need working example for my purpoise
    Last edited by voipp; 12-16-2013 at 03:30 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: extremely hard concurrency error +21

    need threadpool that terminates every thread after N seconds elapsed after this thread was started
    Is there any relationship between the times the threads are started and the N seconds? For example if the threads are started every 0.5 seconds and N is 10 seconds. In other words, any thread that is started at any time would only run for 10 seconds.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: extremely hard concurrency error +21

    Quote Originally Posted by Norm View Post
    Is there any relationship between the times the threads are started and the N seconds? For example if the threads are started every 0.5 seconds and N is 10 seconds. In other words, any thread that is started at any time would only run for 10 seconds.
    no relationship! One more time : im looking for the code that will use threadpool , threads. Threadpool from this code must start M threads at a time, this code must terminate every thread after N seconds elapsed after this threads had been started(started by ThreadPool). Sorry for my bad english

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: extremely hard concurrency error +21

    all of threads run together.
    Why is that a problem if the threads can run at the same time? How does your code example control the starting times of the threads? Are the starting times of the threads important?

    Say thread 1 starts at time 1 and thread 2 starts at time 2, at time 3 there will be two threads running. If N is 5 then thread 1 should stop at time 6 and thread 2 should stop at time 7.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: extremely hard concurrency error +21

    Quote Originally Posted by Norm View Post
    Why is that a problem if the threads can run at the same time? How does your code example control the starting times of the threads? Are the starting times of the threads important?

    Say thread 1 starts at time 1 and thread 2 starts at time 2, at time 3 there will be two threads running. If N is 5 then thread 1 should stop at time 6 and thread 2 should stop at time 7.
    Actually, starting time is not important. What is important is that , lets assume, thread pool starts thread 1 at time 1(and thread 1 must die in N seconds), thread pool starts other threads and they must die in N seconds. In my example you can see thread pool, that starts threads and at the same time starts Bomb thread that in timeout seconds kill the thread:
    Java Code:
    ((ScheduledExecutorService) threadService).schedule(new Bomb(cthread),timeout,TimeUnit.SECONDS);
    Everything just simple.
    PS Otherwise i can use while loop and just check whether threads lifetime elapsed or not and just kill it. But loop seems quiet ugly staff

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: extremely hard concurrency error +21

    Sorry, I don't understand what is wrong with the code you posted. It starts all the threads at the same time and they all run for 10 seconds and then end.
    If you want the threads to start at different times then the code needs to be changed to start them at different times.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: extremely hard concurrency error +21

    Quote Originally Posted by Norm View Post
    Sorry, I don't understand what is wrong with the code you posted. It starts all the threads at the same time and they all run for 10 seconds and then end.
    If you want the threads to start at different times then the code needs to be changed to start them at different times.
    Me too. I dont know what is wrong with the code above,it s not work so i posted it here)

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: extremely hard concurrency error +21

    it s not work
    The code works for me. All the threads are started, execute for a time and then end.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: extremely hard concurrency error +21

    Quote Originally Posted by Norm View Post
    The code works for me. All the threads are started, execute for a time and then end.
    All the threads but N=1 at the same time ?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: extremely hard concurrency error +21

    All the threads are started and execute for 10 seconds each and then end.

    Some problems I see:
    The normal tasks use new threads, they don't use the pool's threads.
    The Bomb tasks use the same pool for its tasks which means if there are no threads left the Bomb task is not started.
    The Bomb tasks are not started when the normal tasks are started. If the normal tasks are waiting for a thread, its Bomb task can already have been started before the normal task is started.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: extremely hard concurrency error +21

    Quote Originally Posted by Norm View Post
    All the threads are started and execute for 10 seconds each and then end.

    Some problems I see:
    The normal tasks use new threads, they don't use the pool's threads.
    The Bomb tasks use the same pool for its tasks which means if there are no threads left the Bomb task is not started.
    The Bomb tasks are not started when the normal tasks are started. If the normal tasks are waiting for a thread, its Bomb task can already have been started before the normal task is started.
    I realize this problems, so i need to be sure that Bomb starts only when thread to be bombed started. shedule(bomb) should be placed in function thread pool starting in new thread.
    And of course , it was my bad, i need to use the other thread pool so killing-threads and ordinar threads dont interfere each other.

    Thanx for help!

Similar Threads

  1. TASLock in concurrency
    By lovelycse in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-19-2013, 06:14 AM
  2. Java in Concurrency
    By bernerd in forum Advanced Java
    Replies: 0
    Last Post: 10-03-2011, 08:20 AM
  3. Practicing Concurrency
    By sunde887 in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 03:27 AM
  4. GUI Concurrency Problems
    By jkhamler in forum Threads and Synchronization
    Replies: 18
    Last Post: 01-20-2010, 03:40 PM
  5. concurrency question
    By diggitydoggz in forum New To Java
    Replies: 4
    Last Post: 01-17-2009, 03:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •