(********* Exercice 1 *********) using System; using System.Linq; using System.Collections.Generic; class Program { public class Thread { public int Workload {get; private set;} public int Pid {get; private set;} private IEnumerator Worker; // an enumerator for these parts static Random rng = new Random(); public Thread() { Workload = 0; Pid = rng.Next() % 100 + 100; Worker = this.TaskLengths().GetEnumerator(); } public void Step() { Workload += Worker.Current; Worker.MoveNext(); } IEnumerable TaskLengths() { while (true) { int load = rng.Next() % 9 + 1; Console.WriteLine("{0} : working for {1} hours (total load : {2})", Pid, load, Workload); yield return load; } } } static void Main(string[] args) { Console.WriteLine("Yield !"); var ListOfThreads = new List(); for (int i = 0; i < 10; i++) { var processor = new Thread(); ListOfThreads.Add(processor); } for (int i=0; i<500; i++) { Thread p = ListOfThreads.Aggregate((curp, next) => { if (next.Workload < curp.Workload) return next; else return curp; }); p.Step(); } } } (********* Exercice 2 *********) import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.SWT; import java.util.ArrayList; import java.util.Arrays; import java.util.stream.Collectors; import java.util.List; import java.util.Random; class Cell { private Button button; // associated widget private List neighbors; // neighbor cells static Random rng = new Random(); public Cell(Composite shell) { button = new Button(shell, SWT.CHECK); neighbors = new ArrayList(); this.setVisible(rng.nextInt(2) == 0); } public void setVisible(boolean b) { button.setSelection(b); } public boolean getVisible() { return button.getSelection(); } public void addNeighbor(Cell c) { this.neighbors.add(c); } public int livingNeighbors() { return (int) neighbors.stream() .filter(d -> d.getVisible()).count(); } } class Board { private List cells; public Board(int height, int width, Shell shell) { Cell[][] board = new Cell[height][width]; for (int i=0; i Arrays.asList(elem).stream()) .collect(Collectors.toList()); } public void evolve() { System.out.println("Evolve"); List living = cells.stream() .filter(c -> { return (!(c.getVisible()) && (c.livingNeighbors() == 3));}) .collect(Collectors.toList()); List dying = cells.stream() .filter(c -> { return ((c.getVisible()) && ((c.livingNeighbors() < 2) || (c.livingNeighbors() > 3)));}) .collect(Collectors.toList()); living.forEach(c -> c.setVisible(true)); dying.forEach (c -> c.setVisible(false)); } } class GameOfLife { public static void main(String[] args) { System.out.println("Game Of Life"); final int size = 14; Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(size, true)); // Game of Life final Board board = new Board(size, size, shell); new Thread(() -> { while(true) { try { Thread.sleep(500); } catch (InterruptedException exn) { } display.asyncExec(() -> { board.evolve(); }); } }).start(); // Standard setup of the swt event loop shell.open(); shell.pack(); while (!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep(); display.dispose(); } }