1 /*
2  * Created on 11-Jul-2004
3  * Created by Paul Gardner
4  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  */
19 
20 package org.gudy.azureus2.core3.util;
21 
22 /**
23  * @author parg
24  *
25  */
26 
27 public abstract class
28 ThreadPoolTask
29 	extends AERunnable
30 {
31 	static final int RELEASE_AUTO = 0x00;
32 	static final int RELEASE_MANUAL = 0x01;
33 	static final int RELEASE_MANUAL_ALLOWED = 0x02;
34 
35 	private int manualRelease;
36 
37 	protected ThreadPool.threadPoolWorker		worker;
38 
39 	public void
setTaskState( String state )40 	setTaskState(
41 		String		state )
42 	{
43 		worker.setState( state );
44 	}
45 
46 	public String
getTaskState()47 	getTaskState()
48 	{
49 		return( worker == null ? "" : worker.getState());
50 	}
51 
52 	public String
getName()53 	getName()
54 	{
55 		return( null );
56 	}
57 
58 	public abstract void
interruptTask()59 	interruptTask();
60 
61 	public void
taskStarted()62 	taskStarted()
63 	{
64 	}
65 
66 	public void
taskCompleted()67 	taskCompleted()
68 	{
69 	}
70 
71 	/**
72 	 * only invoke this method after the first run of the threadpooltask as it is only meant to join
73 	 * on a task when it has child tasks and thus is running in manual release mode
74 	 */
join()75 	synchronized final void join()
76 	{
77 		while(manualRelease != RELEASE_AUTO)
78 		{
79 			try
80 			{
81 				wait();
82 			} catch (Exception e)
83 			{
84 				Debug.printStackTrace(e);
85 			}
86 		}
87 	}
88 
89 	synchronized final void
setManualRelease()90 	setManualRelease()
91 	{
92 		manualRelease = ThreadPoolTask.RELEASE_MANUAL;
93 	}
94 
95 	synchronized final boolean
canManualRelease()96 	canManualRelease()
97 	{
98 		return( manualRelease == ThreadPoolTask.RELEASE_MANUAL_ALLOWED );
99 	}
100 
101 	/**
102 	 * only invoke this method after the first run of the threadpooltask as it is only meant to
103 	 * update the state of a task when it has child tasks and thus is running in manual release mode
104 	 */
isAutoReleaseAndAllowManual()105 	synchronized final boolean isAutoReleaseAndAllowManual()
106 	{
107 		if(manualRelease == RELEASE_MANUAL)
108 			manualRelease = RELEASE_MANUAL_ALLOWED;
109 		return manualRelease == RELEASE_AUTO;
110 	}
111 
releaseToPool()112 	public final synchronized void releaseToPool()
113 	{
114 		// releasing before the initial run finished, so just let the runner do the cleanup
115 		if(manualRelease == RELEASE_MANUAL)
116 			manualRelease = RELEASE_AUTO;
117 		else if(manualRelease == RELEASE_MANUAL_ALLOWED)
118 		{
119 			taskCompleted();
120 			worker.getOwner().releaseManual(this);
121 			manualRelease = RELEASE_AUTO;
122 		} else if(manualRelease == RELEASE_AUTO)
123 			Debug.out("this should not happen");
124 
125 		notifyAll();
126 	}
127 }
128