0% found this document useful (0 votes)
16 views2 pages

Trad Example

The document contains examples of multi-threading in C and Java. In C, a thread is created using pthreads, and in Java, threads are created using the Thread class and Runnable interface. The examples demonstrate how to start threads, execute tasks, and handle thread completion.

Uploaded by

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

Trad Example

The document contains examples of multi-threading in C and Java. In C, a thread is created using pthreads, and in Java, threads are created using the Thread class and Runnable interface. The examples demonstrate how to start threads, execute tasks, and handle thread completion.

Uploaded by

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

#include <stdio.

h>
#include <pthread.h>

// Thread function definition


void* threadFunction(void* args) {
printf("I am a thread function.\n");
return NULL; // Thread functions should return void*
}

int main() {
// Creating thread ID
pthread_t id;
int ret;

// Creating thread
ret = pthread_create(&id, NULL, &threadFunction, NULL);
if (ret == 0) {
printf("Thread created successfully.\n");
} else {
printf("Thread not created.\n");
return 1; // Return error code
}

// Wait for the thread to finish (optional)


pthread_join(id, NULL);

printf("I am main function.\n");


return 0;
}

// Online Java Compiler


// Use this editor to write, compile and run your Java code online

// public class SingleThreadExample {


// public static void main(String[] args) {
// System.out.println("Main thread: " + Thread.currentThread().getName());
// for (int i = 1; i <= 3; i++) {
// System.out.println("Task " + i + " (main thread)");
// }
// }
// }

public class MultiThreadExample {


public static void main(String[] args) {
// Create two threads
Thread thread1 = new Thread(new Task("Thread-1"));
Thread thread2 = new Thread(new Task("Thread-2"));

// Start threads (calls run() method)


thread1.start();
thread2.start();

System.out.println("Main thread: " + Thread.currentThread().getName());


}

static class Task implements Runnable {


private String name;
public Task(String name) {
this.name = name;
}

@Override
public void run() {
for (int i = 1; i <= 3; i++) {
System.out.println(name + ": Task " + i);
try {
Thread.sleep(100); // Simulate work
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
// Example 2: RunnableTest.java

public class RunnableTest {


public static void main(String[] args) {
RunCounter rct = new RunCounter(); // Step 1: Create a Runnable
object
Thread th = new Thread(rct); // Step 2: Pass it to a Thread
object
th.start(); // Step 3: Start the thread
System.out.println("The thread has been started"); // Main thread message
}
}

// A class that implements Runnable


class RunCounter implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Running count: " + i);

try {
Thread.sleep(500); // Pause for 500 milliseconds (optional, for
demo)
} catch (InterruptedException e) {
System.out.println("Thread interrupted: " + e.getMessage());
}
}
}
}

You might also like