Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Fórum Parallel Threads ( With Much Fun )
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Threading; // aljodav // http://rextester.com/users/376 // // This code has much fun. // If you'd rather have no fun, see the other without fun. namespace Rextester { class ParallelThread{ /*ctor*/public ParallelThread(int j){ if(j<0||j>=Maximum)throw new Exception("!-- The index is either too small or too big. --!"); _index=j; } private int _index; // *** A NEW OPERATOR *** public static Action operator +(string s,ParallelThread p){ if(s!="ParallelThread")throw new Exception("!-- This task is NOT a ParallelThread one. --!"); return actions[p._index]; } // *** THE ACTIONS *** private static Action[] actions={ParallelThread0,ParallelThread1,ParallelThread2}; // *** MAXIMUM ACTIONS *** public static readonly int Maximum; /*ctor*/static ParallelThread(){if((Maximum=actions.Length)==0)throw new Exception("!-- You forgot the actions to be taken. :) --!");} // *** THE VERY HANDY ACTION *** public static Action<string,char,int,int> Act=(s,r,q,d)=>{ Array.ForEach(s.ToCharArray(),c=>{ Task.Delay(Environment.TickCount+d&127); Console.WriteLine("{0} {1}",new string(r,q),c); }); }; // *** THE ACTIONS' IMPLEMENTATIONS *** public static void ParallelThread0(){Act("JUST FOR MUCH FUN",'-',16,Thread.CurrentThread.ManagedThreadId);} public static void ParallelThread1(){Act("AND FOR LEARNING,",'+',32,Thread.CurrentThread.ManagedThreadId);} public static void ParallelThread2(){Act("WITH *GOOD* CODE!",'.',48,Thread.CurrentThread.ManagedThreadId);} } class Program { public static int j; public static ParallelThread i(){return new ParallelThread(j);} public static void Main(string[] args) { Task[] tasks= new Task[ParallelThread.Maximum]; for(j=0;j<ParallelThread.Maximum;++j){ tasks[j]=Task.Run( "ParallelThread"+i() ); // <<< *** SEE HERE: JUST FOR FUN :) *** } Task.WaitAll(tasks); } } }
run
|
edit
|
history
|
help
0
Naju Date Format Issue
Math 9.7 (Added Speed Math)
Sort List of Ages Using A Stack
Fórum ➡ Get all combinations for ▶different◀ Id's ♦
D113
Day1
line 1 syntax error
Non-User input - Random Controlled - Guessing Game
V5
ConvertDataTableToHTML