0% found this document useful (0 votes)
10 views

10tp1 Programming-Language

TP1

Uploaded by

larcejen15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

10tp1 Programming-Language

TP1

Uploaded by

larcejen15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Group Members:

Rovic Bilo
Michael Palermo
Ron Nicolai De Castro
Ron Nico De Castro

Java 6 Threads:

class MyThread extends Thread {


public MyThread(String name) {
setName(name);
}

public void run() {


for (int i = 0; i < 5; i++) {
System.out.println(getName() + " is running: " + i);
try {
// Sleep for 500 milliseconds
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(getName() + " was interrupted.");
}
}
}
}

public class ThreadExample {


public static void main(String[] args) {
// Create an array to hold 6 thread objects
MyThread[] threads = new MyThread[6];

// Initialize and start each thread


for (int i = 0; i < 6; i++) {
threads[i] = new MyThread("Thread " + (i + 1));
threads[i].start();
}

// Wait for each thread to finish


for (int i = 0; i < 6; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
System.out.println("Thread " + (i + 1) + " was interrupted.");
}
}

System.out.println("All threads have finished execution.");


}
}

Result:
C# 4 Threads:

using System;
using System.Threading;

class MyThread
{
private string threadName;
private Thread thread;

public MyThread(string name)


{
threadName = name;
thread = new Thread(new ThreadStart(Run));
thread.Name = name;
}

public void StartThread()


{
thread.Start();
}

private void Run()


{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(thread.Name + " is running: " + i);
Thread.Sleep(500);
}
}

public void JoinThread()


{
thread.Join();
}

public bool IsThreadAlive()


{
return thread.IsAlive;
}
}

class ThreadExample
{
static void Main()
{
MyThread t1 = new MyThread("Thread 1");
MyThread t2 = new MyThread("Thread 2");

t1.StartThread();
t2.StartThread();

t1.JoinThread();
t2.JoinThread();

Console.WriteLine("All threads have finished execution.");


}
}

Result:

You might also like