TUGAS 7
Multithreading
Untuk membuat suatu program dengan
menggunakan fitur multitasking atau multithreading. Kita harus
menginisialisasikan suatu objek yang bertipe Thread di java. Pada pemrograman
java, kita dapat membuatnya dalam 2 jalan yaitu :
1.
Dengan cara mengimplementasikan
sebuah interface Runnable.
2.
Dengan cara menurunkan (extends)
kelas Thread.
Artikel ini menjelaskan cara yang pertama.
Pada interface Runnable, hanya ada satu method yang
digunakan yaitu method run(), yang dideklarasi seperti dibawah ini :
public void run( );
Dalam method run() itu, kita dapat memanggil suatu
method lain, menggunakan kelas lain dan variabel pada program utama. Bila sudah
membuat kelas yang mengimplementasikan interface runnable, kamu harus membuat
objek bertipe Thread dalam objek dari kelas itu. Berikut adalah konstruktor
dari Thread :
Thread (Runnable threadObjek);
Dalam konstruktor diatas, threadObjek adalah sebuah
instance dari kelas yang mengimplementasikan interface Runnable. Sampai sini,
untuk memulai menjalankan program dengan multitasking, kita gunakan perintah
method start() objek yang bertipe Thread. Secara otomatis saat memanggil method
start() maka mengeksekusi method dari run().
PEMBAHASAN
1.1.
Contoh
Pemrograman Java
//Class
Multithreading.java
package
Multithreading;
public class
Multithreading implements Runnable {
int count;
String readName;
public Multithreading(String Name) {
count = 0;
readName = Name;
}
public void run() {
System.out.println(readName +"
Dimulai");
try
{
do
{
Thread.sleep(500);
System.out.println("Dalam " +
readName +", Jumlah Count : "+ count);
count++;
}
while(count < 10);
}
catch(InterruptedException e)
{
System.out.println(readName + "
Penghentian Sementara"");
}
System.out.println(readName + "
Selesai");
}
}
//Class Threading.java
package Multithreading;
public class
Threading {
public static void main(String args[]) {
System.out.println("Program Utama
Dijalankan !");
Multithreading multi = new
Multithreading("ThreadLearing");
Thread read = new Thread(multi);
read.start();
do<.o:p>
{
System.out.print("@");
try
{
Thread.sleep(1000);
}
catch(InterruptedException e) {
System.out.println("Program
Utama Dihentikan Sementara");
&nbrp; }
}
while(multi.count != 10);
System.out.println("Program
Utama Selesai !!!");
}
}
Keluaran
dari program diatas :
Program Utama Dijalankan !
@ThreadLearing Dimulai
Dalam ThreadLearing, Jumlah Count :
0
@Dalam ThreadLearing, Jumlah Count :
1
Dalam ThreadLearing, Jumlah Count :
2
@Dalam ThreadLearing, Jumlah Count :
3
Dalam ThreadLearing, Jumlah Count :
4
@Dalam ThreadLearing, Jumlah Count :
5
Dalam ThreadLearing, Jumlah Count :
6
@Dalam ThreadLearing, Jumlah Count :
7
Dalam ThreadLearing, Jumlah Count :
8
@Dalam ThreadLearing, Jumlah Count :
9
ThreadLearing Selesai
Program Utama Selesai !!!
1.2.
Contoh
Pemrograman C#
using
System;
using
System.Threading;
namespace
Multithreading
{
public
class Multithreading {
public int count;
string readName;
public Multithreading(string Name) {
count = 0;
readName = Name;
}
//Entry point of thread.
public void run() {
Console.WriteLine(readName + " Dimulai");
do
{
Thread.Sleep(500);
Console.WriteLine("Dalam " + readName + ",Jumlah Count " + count);
count++;
}
while(count < 10);
Console.WriteLine(readName + " Selesai");
}
}
public
class Threading {
public static void Main() {
Console.WriteLine("Program Utama Dijalankan !");
// First, construct a Multithreading object.
Multithreading
multi = new Multithreading ("ThreadLearing");
//Next, construct a thread from that object.
Thread read = new Thread(new ThreadStart(multi.run));
//Finally, start execution of the thread.
read.Start();
do
{
Console.Write("@");
Thread.Sleep(1000);
}
while (multi.count != 10);
Console.WriteLine("Program Utama Selesai !!!");
Console.Read();
}
}
}
1.3. Contoh Pemrograman C++
//Create C++ Language below into a Notepad or software Language C++ and save it as thread.h
//before
do programming, make sure your software Language C++ has been installed
//when
do you not yet has software Language C++, you
can download one of the software Language C++
at http://www.bloodshed.net/dev/devcpp.html
//copy
or cut file thread.h and paste into the folder name is include
//Example: (C:\Program
Files\Dev-Cpp\MinGW\include).
//thread.h
#ifndef KBCAFE_THREAD_H
#define KBCAFE_THREAD_H
#ifdef _WIN32
#ifndef _WINDOWS_
#include <windows.h>
#endif
#else
#error _WIN32 must be defined
before you include thread.h
#endif
namespace kbcafe
{
class thread
{
#ifdef _WIN32
static DWORD WINAPI
ThreadFunc(LPVOID pv)
{
try
{
(reinterpret_cast<thread
*>(pv))->run();
}
catch(...)
{
}
return 0;
}
#elif defined(__sun)
#else
#endif
public:
#ifdef _WIN32
typedef DWORD threadid;
#elif defined(__sun)
#else
#endif
thread()
{
}
virtual ~thread()
{
}
static threadid getthreadid()
{
#ifdef _WIN32
return ::GetCurrentThreadId();
#elif defined(__sun)
#else
#endif
}
static void sleep(long
milliseconds=1)
{
#ifdef _WIN32
::Sleep(milliseconds);
#elif defined(__sun)
#else
#endif
}
threadid start()
{
threadid id;
#ifdef _WIN32
::CreateThread(NULL, 0, ThreadFunc,
this, 0, &id);
#elif defined(__sun)
#else
#endif
return id;
}
virtual void run()=0;
};
};
#endif
//Thread
Sample
#include <iostream>
#include "thread.h"
class MyThread : public
kbcafe::thread {
public:
virtual void run() {
std::cout <<
"Hello" << std::endl;
};
};
int main(int argc, char* argv[]) {
MyThread thread;
thread.start();
::Sleep(1000);
return 0;
}
//Example
C++ Language
#include <iostream>
#include "thread.h"
class MyThread : public kbcafe::thread
{
public:
virtual void run() {
for (int i=0;i<100;i++)
std::cout <<
"Hello" << std::endl;
};
};
int main(int argc, char* argv[]) {
MyThread thread;
thread.start();
for (int i=0;i<100;i++)
std::cout <<
"Hello" << std::endl;
return 0;
}
//Create C++ Language below into a Notepad or Software Language C++ and save it as criticalsection.h
//before
do programming, make sure your software Language C++ has been installed
//when
do you not yet has software Language C++, you
can download one of the software Language C++
at http://www.bloodshed.net/dev/devcpp.html
//copy
or cut file criticalsection.h and paste into the folder name is include
//Example: (C:\Program
Files\Dev-Cpp\MinGW\include).
//criticalsection.h
#ifndef KBCAFE_CRITICALSECTION_H
#define KBCAFE_CRITICALSECTION_H
#ifdef _WIN32
#ifndef _WINDOWS_
#include <windows.h>
#endif
#else
#error _WIN32 must be defined
before you include criticalsection.h
#endif
namespace kbcafe
{
class criticalsection
{
#ifdef _WIN32
typedef CRITICAL_SECTION cs;
#elif defined(__sun)
#else
#endif
cs m_cs;
public:
criticalsection()
{
#ifdef _WIN32
::InitializeCriticalSection(&m_cs);
#elif defined(__sun)
#else
#endif
}
~criticalsection()
{
#ifdef _WIN32
::DeleteCriticalSection(&m_cs);
#elif defined(__sun)
#else
#endif
}
void enter()
{
#ifdef _WIN32
::EnterCriticalSection(&m_cs);
#elif defined(__sun)
#else
#endif
}
void leave()
{
#ifdef _WIN32
::LeaveCriticalSection(&m_cs);
#elif defined(__sun)
#else
#endif
}
};
};
#endif
//Example
C++ Language
#include <iostream>
#include "thread.h"
#include
"criticalsection.h"
kbcafe::criticalsection cs;
class MyThread : public
kbcafe::thread {
public:
virtual void run() {
for (int i=0;i<100;i++) {
cs.enter();
std::cout <<
"Hello" << std::endl;
cs.leave();
}
};
};
int main(int argc, char* argv[]) {
MyThread thread;
thread.start();
for (int i=0;i<100;i++) {
cs.enter();
std::cout <<
"Hello" << std::endl;
cs.leave();
}
return 0;
}
Tidak ada komentar:
Posting Komentar