Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7.  
  8. namespace Lab1_nowe
  9. {
  10.  
  11. public interface IRunnable
  12. {
  13. void Run();
  14. IEnumerator<float> CoroutineUpdate();
  15. bool HasFinished
  16. {
  17. get;
  18. set;
  19. }
  20.  
  21. }
  22.  
  23. public abstract class Agent : IRunnable
  24. {
  25. public int czestotliwosc = 10;
  26.  
  27. public int ID;
  28. public static int IDAgent;
  29. public abstract void Update();
  30.  
  31. public void Run()
  32. {
  33. while (!HasFinished)
  34. {
  35. double opoznienie = 1000 / czestotliwosc;
  36. Thread.Sleep((int)opoznienie);
  37. Update();
  38.  
  39. }
  40. }
  41.  
  42. public IEnumerator<float> CoroutineUpdate()
  43. {
  44. while(!HasFinished)
  45. {
  46. Update();
  47. yield return 1;
  48. }
  49. yield break;
  50. }
  51. public bool HasFinished
  52. {
  53. get;
  54. set;
  55. }
  56. }
  57.  
  58. public class ConstantCountingAgent : Agent
  59. {
  60. private int licz;
  61. public ConstantCountingAgent()
  62. {
  63. ID=++IDAgent;
  64.  
  65. }
  66. override public void Update()
  67. {
  68.  
  69. if (licz<=10)
  70. {
  71. HasFinished = false;
  72. // Console.WriteLine("stan licznika: " + licz);
  73.  
  74. }else
  75. {
  76. Console.WriteLine("ConstantCountingAgent zakończył prace: ID = " + ID);
  77. HasFinished = true;
  78. }
  79. licz++;
  80. }
  81. }
  82.  
  83. public class CountingAgent : Agent
  84. {
  85. public int licz;
  86. public CountingAgent()
  87. {
  88. ID = ++IDAgent;
  89.  
  90. }
  91. override public void Update()
  92. {
  93.  
  94. if (licz <= ID)
  95. {
  96. HasFinished = false;
  97. //Console.WriteLine("stan licznika: " + licz);
  98. }
  99. else
  100. {
  101. Console.WriteLine("CountingAgent zakończył prace: ID = " + ID);
  102. HasFinished = true;
  103. }
  104. licz++;
  105. }
  106.  
  107. }
  108.  
  109. public class SineGeneratingAgent : Agent
  110. {
  111. float Output
  112. {
  113. get;
  114. set;
  115. }
  116. double czas;
  117. double czas1;
  118. int licz;
  119. public SineGeneratingAgent()
  120. {
  121. ID = ++IDAgent;
  122.  
  123. }
  124. override public void Update()
  125. {
  126.  
  127. czas = czas + (1 / Convert.ToDouble(czestotliwosc));
  128. if (ID%10 >= czas)
  129. {
  130. HasFinished = false;
  131. // Console.WriteLine("{0}", Math.Round(Math.Sin(czas), 2));
  132. }
  133. else
  134. {
  135. Console.WriteLine("SineGeneratingAgent zakończył prace: ID = " + ID);
  136. HasFinished = true;
  137. }
  138.  
  139.  
  140.  
  141. }
  142.  
  143. }
  144.  
  145.  
  146.  
  147. class Program
  148. {
  149.  
  150. static List<IRunnable> list_ob = new List<IRunnable>();
  151. static List<IRunnable> list_liczb = new List<IRunnable>();
  152. static List<IRunnable> GenerateRunnables()
  153. {
  154. Random gen = new Random();
  155. for (int i = 0; i < 1000; i++)
  156. {
  157. list_liczb[i] = gen.Next(1, 49);
  158. Console.Write("{0}; ", list_liczb[i]);
  159. }
  160. List<IRunnable> lista = new List<IRunnable>();
  161.  
  162. for (int i = 0; i < 10; i++)
  163. {
  164. lista.Add(new ConstantCountingAgent());
  165. lista.Add(new CountingAgent());
  166. lista.Add(new SineGeneratingAgent());
  167. }
  168. return lista;
  169. }
  170.  
  171. public static void RunThreads(List<IRunnable> lista)
  172. {
  173.  
  174. for (int i=0; i<lista.Count; i++)
  175. {
  176. Thread watek = new Thread(lista.ElementAt(i).Run);
  177. watek.Start();
  178. }
  179. }
  180. static public void RunFibres(IEnumerable<IRunnable> wlokna)
  181. {
  182. var functions = wlokna.Select(n => n.CoroutineUpdate());
  183. while (!(wlokna.All(m => m.HasFinished)))
  184. {
  185. foreach (var fiber in functions)
  186. {
  187. if (!wlokna.All(m => m.HasFinished))
  188. {
  189. fiber.MoveNext();
  190. }
  191. }
  192. System.Threading.Thread.Sleep(100);
  193.  
  194. }
  195. }
  196.  
  197. static void Main(string[] args)
  198. {
  199.  
  200.  
  201. list_ob = GenerateRunnables();
  202. //RunThreads(GenerateRunnables());
  203. RunFibres(list_ob);
  204. Console.ReadLine();
  205. }
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement