using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
   7:  
namespace SynchronizationSamples
   9: {
class SharedState
  11:     {
int state = 0;
object();
int State 
  15:         { 
return state;}
value; }
  18:         }
  19:  
int IncrementState()
  21:         {
ref state);
  23:  
  24:         }
  25:     }
  26:  
class Job
  28:     {
  29:         SharedState sharedState;
public Job(SharedState sharedState)
  31:         {
this.sharedState = sharedState;
  33:         }
  34:  
void DoTheJob()
  36:         {
int i = 0; i < 50000;i++ )
  38:             {
  39:                sharedState.IncrementState();
  40:             }
  41:         }
  42:     }
class Program
  44:     {
string[] args)
  46:         {
int numTasks = 20;
new SharedState();
new Task[numTasks];
  50:  
int i = 0; i < numTasks; i++)
  52:             {
new Job(state).DoTheJob);
  54:                 tasks[i].Start();
  55:             }
  56:  
int i = 0; i < numTasks;i++ )
  58:             {
  59:                 tasks[i].Wait();
  60:             }
,state.State);
  62:             Console.Read();
  63:         }
  64:     }
  65: }

相关文章:

  • 2021-07-02
  • 2021-07-25
  • 2021-12-15
  • 2022-12-23
  • 2021-09-04
  • 2021-09-18
猜你喜欢
  • 2022-02-06
  • 2021-07-27
  • 2021-11-05
  • 2021-08-26
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
相关资源
相似解决方案