public
class
StackScheduler
:
IScheduler
{
protected
Stack
<Task>
_stack;
public
StackScheduler
()
{
_stack
=
new
Stack<
Task>();
}
public
void
Add(
Task
t
)
{
if
(!Contains
(t))
{
_stack.Push
(t);
}
}
public
void
Remove(
Task
t
)
{
_stack.Pop
();
}
public
bool
Contains(
Task
t
)
{
bool
found
=
false;
foreach
(var
task
in
_stack
)
{
if
(t
.AsyncState
!=
null
&&
t
.AsyncState.
Equals(task
.AsyncState))
{
found
=
true
;
break;
}
}
return
found
;
}
public
bool
Contains(
Task
t
,
EqualityComparer<
Task>
comp
)
{
throw
new
NotImplementedException();
}
public
IEnumerator
<Task>
GetEnumerator()
{
return
new
SchedulerEnumerator(
this);
}
IEnumerator
IEnumerable
.GetEnumerator()
{
return
new
SchedulerEnumerator(
this);
}
public
int
Count
{
get
{
return
_stack.
Count; }
}
public
Task
this[
int
index]
{
get
{
return
(Task)
_stack.ToArray
()[index]; }
set
{
_stack
.ToArray()[index]
= value; }
}
}
public
class
DynamicQueueScheduler
:
IScheduler
{
protected
List
<Task>
_queue;
public
DynamicQueueScheduler
()
{
_queue
=
new
List<
Task>();
}
public
virtual
void
Add(Task
t)
{
Task
oldTask
=
null;
if
(Contains
(t,
out
oldTask
))
{
_queue.Remove
(oldTask);
}
_queue.Add
(t);
}
public
virtual
void
Remove(Task
t)
{
_queue.Remove
(t);
}
public
virtual
bool
Contains(Task
t)
{
Task
oldTask
=
null;
bool
found
=
Contains(
t,
out
oldTask);
return
found
;
}
public
virtual
bool
Contains(Task
t,
out
Task
oldTask)
{
bool
found
=
false;
oldTask
=
null
;
foreach
(var
task
in
_queue
)
{
if
(t
.AsyncState
!=
null
&&
t
.AsyncState.
Equals(task
.AsyncState))
{
oldTask
=
task
;
found
=
true
;
break;
}
}
return
found
;
}
public
virtual
bool
Contains(Task
t,
EqualityComparer<Task
>
comp)
{
throw
new
NotImplementedException();
}
public
IEnumerator
<Task>
GetEnumerator()
{
return
new
SchedulerEnumerator(
this);
}
IEnumerator
IEnumerable
.GetEnumerator()
{
return
new
SchedulerEnumerator(
this);
}
public
int
Count
{
get
{
return
_queue.
Count; }
}
public
Task
this[
int
index]
{
get
{
return
(Task)
_queue[index]; }
set
{
_queue
[index] =
value; }
}
}
4.Dynamic LIFO
public
class
DynamicStackScheduler
:
IScheduler
{
protected
List
<Task>
_queue;
public
DynamicStackScheduler
()
{
_queue
=
new
List<
Task>();
}
public
void
Add(
Task
t
)
{
Task
oldTask
=
null;
if
(Contains
(t,
out
oldTask
))
{
_queue.Remove
(oldTask);
}
_queue.Insert
(0,t);
}
public
void
Remove(
Task
t
)
{
_queue.Remove
(t);
}
public
bool
Contains(
Task
t
)
{
Task
oldTask
=
null;
bool
found
=
Contains(
t,
out
oldTask);
return
found
;
}
public
bool
Contains(
Task
t
,
out
Task
oldTask
)
{
bool
found
=
false;
oldTask
=
null
;
foreach
(var
task
in
_queue
)
{
if
(t
.AsyncState
!=
null
&&
t
.AsyncState.
Equals(task
.AsyncState))
{
oldTask
=
task
;
found
=
true
;
break;
}
}
return
found
;
}
public
bool
Contains(
Task
t
,
EqualityComparer<
Task>
comp
)
{
throw
new
NotImplementedException();
}
public
IEnumerator
<Task>
GetEnumerator()
{
return
new
SchedulerEnumerator(
this);
}
IEnumerator
IEnumerable
.GetEnumerator()
{
return
new
SchedulerEnumerator(
this);
}
public
int
Count
{
get
{
return
_queue.
Count; }
}
public
Task
this[
int
index]
{
get
{
return
(Task)
_queue[index]; }
set
{
_queue
[index] =
value; }
}
}
测试代码
class
Program
{
static
Queue
<int>
_queue
=
new
Queue<
int>();
//static TaskFactory _factory = new TaskFactory(new TaskSchedulerBase<QueueScheduler>());
//static TaskFactory _factory = new TaskFactory(new TaskSchedulerBase<StackScheduler>());
//static TaskFactory _factory = new TaskFactory(new TaskSchedulerBase<DynamicQueueScheduler>());
//static TaskFactory _factory = new TaskFactory(new TaskSchedulerBase<DynamicStackScheduler>());
static
TaskFactory
_factory
=
new
TaskFactory
(new
TaskSchedulerBase<DynamicQueueScheduler
>());
static
void
Main(
string[]
args
)
{
var
thread1
=
new
Thread(Producer
);
var
thread2
=
new
Thread(Consumer
);
thread1.Start
();
thread2.Start
();
Console.ReadKey
();
}
static
void
Producer()
{
for
(int
i
= 0;
i
< 7;
i
++)
{
_queue.Enqueue
(i);
}
_queue.Enqueue
(3);
_queue.Enqueue
(2);
}
static
void
Consumer()
{
while
(true
)
{
if
(_queue
.Count
> 0)
{
foreach
(var
i
in
_queue
)
{
_factory.StartNew
((s) =>
{
Console.Write
("{0}
on thread
{1}
{2}\n",
s, Thread.CurrentThread
.ManagedThreadId,
DateTime.Now
.ToLongTimeString());
},
i);
}
_queue.Clear
();
}
else
{
Thread.Sleep
(1);
}
}
}
}