Thread adalah suatu pengontrol aliran program dengan menggunakan kendali tunggal.
contoh thread pada pemrograman java :
class TestSinkronisasi {
private java.util.Random random = new java.util.Random();
public void callMe(String data) {
System.out.print("[");
67
try{
Thread.sleep(random.nextInt(200));
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.print(data);
try{
Thread.sleep(random.nextInt(200));
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("]");
}
}
class ThreadBaru extends Thread {
private String data;
private TestSinkronisasi obj;
public ThreadBaru(TestSinkronisasi obj,String data) {
this.obj = obj;
this.data = data;
start();
}
public void run() {
obj.callMe(data);
}
}
class DemoThread {
public static void main(String[] args) {
TestSinkronisasi obj = new TestSinkronisasi();
ThreadBaru thread1 = new ThreadBaru(obj,"Superman");
ThreadBaru thread2 = new ThreadBaru(obj,"Batman");
ThreadBaru thread3 = new ThreadBaru(obj,"Spiderman");
//tunggu hingga semua child thread selesai dieksekusi
try{
68
thread1.join();
thread2.join();
thread3.join();
}catch(InterruptedException e) {
System.out.println("Thread utama diinterupsi " + e);
}
}
}
Ada juga alternatif lain sebagai berikut :
#define _REENTRANT
#include <stdio.h>
#include <thread.h>
/* Prototype for thread subroutine */
void *counter(void *);
int count;
mutex_t count_lock;
main()
{
char str[80];
thread_t ctid;
/* create the thread counter subroutine */
thr_create(NULL, 0, counter, 0, THR_NEW_LWP|THR_DETACHED, &ctid);
while(1) {
gets(str);
thr_suspend(ctid);
mutex_lock(&count_lock);
printf("\n\nCOUNT = %d\n\n", count);
mutex_unlock(&count_lock);
thr_continue(ctid);
}
return(0);
}
void *counter(void *arg)
{
int i;
while (1) {
printf("."); fflush(stdout);
mutex_lock(&count_lock);
count++;
for (i=0;i<50000;i++);
mutex_unlock(&count_lock);
for (i=0;i<50000;i++);
}
return((void *)0);
}
private java.util.Random random = new java.util.Random();
public void callMe(String data) {
System.out.print("[");
67
try{
Thread.sleep(random.nextInt(200));
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.print(data);
try{
Thread.sleep(random.nextInt(200));
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("]");
}
}
class ThreadBaru extends Thread {
private String data;
private TestSinkronisasi obj;
public ThreadBaru(TestSinkronisasi obj,String data) {
this.obj = obj;
this.data = data;
start();
}
public void run() {
obj.callMe(data);
}
}
class DemoThread {
public static void main(String[] args) {
TestSinkronisasi obj = new TestSinkronisasi();
ThreadBaru thread1 = new ThreadBaru(obj,"Superman");
ThreadBaru thread2 = new ThreadBaru(obj,"Batman");
ThreadBaru thread3 = new ThreadBaru(obj,"Spiderman");
//tunggu hingga semua child thread selesai dieksekusi
try{
68
thread1.join();
thread2.join();
thread3.join();
}catch(InterruptedException e) {
System.out.println("Thread utama diinterupsi " + e);
}
}
}
Ada juga alternatif lain sebagai berikut :
#define _REENTRANT
#include <stdio.h>
#include <thread.h>
/* Prototype for thread subroutine */
void *counter(void *);
int count;
mutex_t count_lock;
main()
{
char str[80];
thread_t ctid;
/* create the thread counter subroutine */
thr_create(NULL, 0, counter, 0, THR_NEW_LWP|THR_DETACHED, &ctid);
while(1) {
gets(str);
thr_suspend(ctid);
mutex_lock(&count_lock);
printf("\n\nCOUNT = %d\n\n", count);
mutex_unlock(&count_lock);
thr_continue(ctid);
}
return(0);
}
void *counter(void *arg)
{
int i;
while (1) {
printf("."); fflush(stdout);
mutex_lock(&count_lock);
count++;
for (i=0;i<50000;i++);
mutex_unlock(&count_lock);
for (i=0;i<50000;i++);
}
return((void *)0);
}