Skip to content
Snippets Groups Projects
Unverified Commit 732ade8d authored by Hamza Remmal's avatar Hamza Remmal
Browse files

Add examples in lecture1 slides

parent 01e1d694
No related branches found
No related tags found
No related merge requests found
package lecture1
class MyThread(val sleepiness: Int) extends Thread:
override def run: Unit =
var i: Int = 0
......
// Lecture 1 : Page 11 / 15
package lecture1
class ThreadReturning[A](toRun: => A) extends Thread :
var result: A = compiletime.uninitialized
override def run(): Unit =
result = toRun
def joinMe: A =
join()
result
def thread[A](toRun: => A): ThreadReturning[A] =
val t = ThreadReturning(toRun)
t.start()
t
\ No newline at end of file
// Lecture 1 : Page 12/15
package lecture1
def countLots: Long =
var s,i: Long = 0
while i < 7 do
var j: Long = 0
while j < 900000000 do
j = j + 1
s = s + i + j
i = i + 1
//println(i)
s
def testCounts: (Long, Long) =
val (x, y) = (thread(countLots), thread(countLots))
(x.joinMe, y.joinMe)
def time[A](computation: => A): (A, Long) =
val timePre: Long = System.currentTimeMillis
(computation, System.currentTimeMillis - timePre)
\ No newline at end of file
// Lecture 1 : 14/15
package lecture1;
class ExampleThread extends Thread {
int sleepiness;
public void run() {
int i = 0;
while (i < 8) {
System.out.println("Thread" + getName() + " has counter " + i);
i++;
try {sleep(sleepiness);}
catch (InterruptedException e) {
System.out.println("Thread was interrupted.");
}
}
}
public static void main(String[] args) {
ExampleThread t1 = new ExampleThread();
t1.sleepiness = 2;
ExampleThread t2 = new ExampleThread();
t2.sleepiness = 3;
System.out.println("Little threads did not start yet!");
t1.start();
t2.start();
System.out.println("Parent thread running!");
try {
sleep(29);
} catch (InterruptedException e) {
System.out.println("Parent thread was interrupted.");
}
System.out.println("Parent thread running!");
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
System.out.println("A thread was interruped!");
}
System.out.println("Done executing thread.");
}
}
package lecture1
def parallel[A, B](taskA: => A, taskB: => B): (A, B) =
val tB: Task[B] = task {
taskB
}
val tA: A = taskA
(tA, tB.join)
\ No newline at end of file
package lecture1
def task[A](c: => A) : Task[A] = ???
trait Task[A] :
def join: A
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment