OOP : Multithreading Pada C#, JAVA dan C++

Selamat datang di RESTOPROGRAM penyedia sajian tutorial pemrograman terkini. Masih ane sajikan hidangan spesial OOP. Untuk yang ketujuh ane sajikan hidangan BAKSO C# dengan cita rasa Multithreading Class Pada C#, JAVA dan C++. Langsung saja ane bahas mengenai sajian ini.

Multithreading C#, JAVA dan C++Multithreading adalah cara pengeksekusian yang mengizinkan beberapa thread terjadi dalam sebuah proses, saling berbagi sumber daya tetapi dapat dijalankan secara independen. Keuntungan dari sistem yang menerapkan multithreading dapat kita kategorikan menjadi 4 bagian:

  1. Responsif : Aplikasi interaktif menjadi tetap responsif meskipun sebagian dari program sedang diblok atau melakukan operasi lain yang panjang. Umpamanya, sebuah thread dari web browser dapat melayani permintaan pengguna sementara thread yang lain berusaha menampilkan gambar.
  2. Berbagi sumber daya : Beberapa thread yang melakukan proses yang sama akan berbagi sumber daya. Keuntungannya adalah mengizinkan sebuah aplikasi untuk mempunyai beberapa thread yang berbeda dalam lokasi memori yang sama.
  3. Ekonomis : Pembuatan sebuah proses memerlukan pengalokasian memori dan sumber daya. Alternatifnya adalah dengan menggunakan thread , karena thread membagi memori dan sumber daya yang dimilikinya sehingga lebih ekonomis untuk membuat thread dan context switching thread . Akan susah mengukur perbedaan waktu antara thread dan switch, tetapi secara umum pembuatan dan pengaturan proses akan memakan waktu lebih lama dibandingkan dengan thread . Pada Solaris, pembuatan proses memakan waktu 30 kali lebih lama dibandingkan pembuatan thread sedangkan proses context switch 5 kali lebih lama dibandingkan context switching thread.
  4. Utilisasi arsitektur multiprosesor : Keuntungan dari multithreading dapat sangat meningkat pada arsitektur multiprosesor, dimana setiap thread dapat berjalan secara paralel di atas procesor yang berbeda. Pada arsitektur processor tunggal, CPU menjalankan setiap thread secara bergantian tetapi hal ini berlangsung sangat cepat sehingga menciptakan ilusi paralel, tetapi pada kenyataanya hanya satu thread yang dijalankan CPU pada satu-satuan waktu.
  • Contoh Multithreading C#
using System;
using System.Threading;

namespace oop_multithread
{
class belThreading
{
public void cobaThread1()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Thread 1 {0}", i);
Thread.Sleep(1000);
}
}
public void cobaThread2()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Thread 2 {0}", i);
Thread.Sleep(1000);
}
}
}
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Multithread");

belThreading tid1 = new belThreading();

tid1.cobaThread1();
tid1.cobaThread2();

Console.WriteLine("End");
Console.ReadKey(true);
}
}
}
  • Hasil Multithreading C#

multithreading1

  • Contoh Multithreading JAVA
package oop_multithread;
/**
*
* @author Imamoto
*/
class belThreading extends Thread
{
public void cobaThread1()
{
try
{
for (int i = 0; i < 10; i++)
{
System.out.println("Thread 1 " + i);
Thread.sleep(1000);
}
}
catch(Exception e)
{

}
}
public void cobaThread2()
{
try
{
for (int i = 0; i < 10; i++)
{
System.out.println("Thread 2 " + i);
Thread.sleep(1000);
}
}
catch(Exception e)
{

}
}
}
public class Main {

public static void main(String[] args) {
System.out.println("MultiThreading");

belThreading tid1 = new belThreading();

tid1.cobaThread1();
tid1.cobaThread2();

System.out.println("End");
}
}
  • Hasil Multithreading JAVA

multithreading2

  • Contoh Multithreading C++

Khusus C++ untuk membuat Multithreading harus menggunakan header file, dalam contoh kode program berikut memanfaatkan header file buatan programmer lain. Referensinya ada pada.

ou_thread.h

namespace openutils {
class ThreadException;
class Mutex;

/** class Thread
* Represents a thread of execution
* in the process. To create a new Thread
* write an inherited class ot Thread and
* override the run() function
**/
class Thread {

private:
// unsigned long* to the low-level thread object
unsigned long* m_hThread;
// a name to identify the thread
string m_strName;

public:
Thread();
Thread(const char* nm);
virtual ~Thread();
void setName(const char* nm);
string getName() const;
void start();
virtual void run();
void sleep(long ms);
void suspend();
void resume();
void stop();

void setPriority(int p);

bool wait(const char* m,long ms=5000);
void release(const char* m);

public:
// Thread priorities
static const int P_ABOVE_NORMAL;
static const int P_BELOW_NORMAL;
static const int P_HIGHEST;
static const int P_IDLE;
static const int P_LOWEST;
static const int P_NORMAL;
static const int P_CRITICAL;
private:
};// class Thread

/** class Mutex
* Represents a Mutex object to synchronize
* access to shaed resources.
**/
class Mutex {
private:
// unsigned long* to the low-level mutex object
unsigned long* m_hMutex;
// name to identify the mutex
string m_strName;
public:
Mutex();
Mutex(const char* nm);
void create(const char* nm);
unsigned long* getMutexHandle();
string getName();
void release();
~Mutex();
};

/** class ThreadException
* thrown by Thread and Mutex function
* calls
**/
class ThreadException {
private:
string msg;
public:
ThreadException(const char* m);
string getMessage() const;
};
} // namespace openutils

// global function called by the thread object.
// this in turn calls the overridden run()
extern "C" {
unsigned int _ou_thread_proc(void* param);}

ou_thread.cpp

#include
using namespace std;

#include

#include "ou_thread.h"
using namespace openutils;

const int Thread::P_ABOVE_NORMAL = THREAD_PRIORITY_ABOVE_NORMAL;
const int Thread::P_BELOW_NORMAL = THREAD_PRIORITY_BELOW_NORMAL;
const int Thread::P_HIGHEST = THREAD_PRIORITY_HIGHEST;
const int Thread::P_IDLE = THREAD_PRIORITY_IDLE;
const int Thread::P_LOWEST = THREAD_PRIORITY_LOWEST;
const int Thread::P_NORMAL = THREAD_PRIORITY_NORMAL;
const int Thread::P_CRITICAL = THREAD_PRIORITY_TIME_CRITICAL;

/**@ The Thread class implementation
**@/

/** Thread()
* default constructor
**/
Thread::Thread() {
m_hThread = NULL;
m_strName = "null";
}

/** Thread(const char* nm)
* overloaded constructor
* creates a Thread object identified by "nm"
**/
Thread::Thread(const char* nm) {
m_hThread = NULL;
m_strName = nm;
}

Thread::~Thread() {
if(m_hThread != NULL) {
stop();
}
}

/** setName(const char* nm)
* sets the Thread object's name to "nm"
**/
void Thread::setName(const char* nm) {
m_strName = nm;
}

/** getName()
* return the Thread object's name as a string
**/
string Thread::getName() const {
return m_strName;
}

/** run()
* called by the thread callback _ou_thread_proc()
* to be overridden by child classes of Thread
**/
void Thread::run() {
// Base run
}

/** sleep(long ms)
* holds back the thread's execution for
* "ms" milliseconds
**/
void Thread::sleep(long ms) {
Sleep(ms);
}

/** start()
* creates a low-level thread object and calls the
* run() function
**/
void Thread::start() {
DWORD tid = 0;
m_hThread = (unsigned long*)CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)_ou_thread_proc,(Thread*)this,0,&tid);
if(m_hThread == NULL) {
throw ThreadException("Failed to create thread");
}else {
setPriority(Thread::P_NORMAL);
}
}

/** stop()
* stops the running thread and frees the thread handle
**/
void Thread::stop() {
if(m_hThread == NULL) return;
WaitForSingleObject(m_hThread,INFINITE);
CloseHandle(m_hThread);
m_hThread = NULL;
}

/** setPriority(int tp)
* sets the priority of the thread to "tp"
* "tp" must be a valid priority defined in the
* Thread class
**/
void Thread::setPriority(int tp) {
if(m_hThread == NULL) {
throw ThreadException("Thread object is null");
}else {
if(SetThreadPriority(m_hThread,tp) == 0) {
throw ThreadException("Failed to set priority");
}
}
}

/** suspend()
* suspends the thread
**/
void Thread::suspend() {
if(m_hThread == NULL) {
throw ThreadException("Thread object is null");
}else {
if(SuspendThread(m_hThread) < 0) {
throw ThreadException("Failed to suspend thread");
}
}
}

/** resume()
* resumes a suspended thread
**/
void Thread::resume() {
if(m_hThread == NULL) {
throw ThreadException("Thread object is null");
}else {
if(ResumeThread(m_hThread) < 0) { throw ThreadException("Failed to resume thread"); } } } /** wait(const char* m,long ms) * makes the thread suspend execution until the * mutex represented by "m" is released by another thread. * "ms" specifies a time-out for the wait operation. * "ms" defaults to 5000 milli-seconds **/ bool Thread::wait(const char* m,long ms) { HANDLE h = OpenMutex(MUTEX_ALL_ACCESS,FALSE,m); if(h == NULL) { throw ThreadException("Mutex not found"); } DWORD d = WaitForSingleObject(h,ms); switch(d) { case WAIT_ABANDONED: throw ThreadException("Mutex not signaled"); break; case WAIT_OBJECT_0: return true; case WAIT_TIMEOUT: throw ThreadException("Wait timed out"); break; } return false; } /** release(const char* m) * releases the mutex "m" and makes it * available for other threads **/ void Thread::release(const char* m) { HANDLE h = OpenMutex(MUTEX_ALL_ACCESS,FALSE,m); if(h == NULL) { throw ThreadException("Invalid mutex handle"); } if(ReleaseMutex(h) == 0) { throw ThreadException("Failed to release mutex"); } } /**@ The Mutex class implementation **@/ /** Mutex() * default constructor **/ Mutex::Mutex() { m_hMutex = NULL; m_strName = ""; } /** Mutex(const char* nm) * overloaded constructor * creates a Mutex object identified by "nm" **/ Mutex::Mutex(const char* nm) { m_strName = nm; m_hMutex = (unsigned long*)CreateMutex(NULL,FALSE,nm); if(m_hMutex == NULL) { throw ThreadException("Failed to create mutex"); } } /** create(const char* nm) * frees the current mutex handle. * creates a Mutex object identified by "nm" **/ void Mutex::create(const char* nm) { if(m_hMutex != NULL) { CloseHandle(m_hMutex); m_hMutex = NULL; } m_strName = nm; m_hMutex = (unsigned long*)CreateMutex(NULL,FALSE,nm); if(m_hMutex == NULL) { throw ThreadException("Failed to create mutex"); } } /** getMutexHandle() * returns the handle of the low-level mutex object **/ unsigned long* Mutex::getMutexHandle() { return m_hMutex; } /** getName() * returns the name of the mutex **/ string Mutex::getName() { return m_strName; } void Mutex::release() { if(m_hMutex != NULL) { CloseHandle(m_hMutex); } } Mutex::~Mutex() { /*if(m_hMutex != NULL) { CloseHandle(m_hMutex); }*/ } // ThreadException ThreadException::ThreadException(const char* m) { msg = m; } string ThreadException::getMessage() const { return msg; } // global thread caallback unsigned int _ou_thread_proc(void* param) { Thread* tp = (Thread*)param; tp->run();
return 0;
}

Main Program

#include
#include
#include

using namespace std;
#include "ou_thread.h"
using namespace openutils;

class MyThread : public Thread {
private:
int m_nCount;
public:
MyThread(int n,const char* nm) {
Thread::setName(nm);
m_nCount = n;
}
void run() {
for(int i=0;i<m_nCount;i++) {
cout << getName().c_str()<<" "<<i<< endl;
sleep(1000);
}
}
};

int main(int argc, char *argv[])
{
cout<<"Multithread"<<endl; Thread *t1 = new MyThread(10,"Thread 1"); Thread *t2 = new MyThread(10,"Thread 2"); try { t1->start();
t1->stop();
t2->start();
t2->stop();
cout<<"End"<<endl;
}
catch(ThreadException ex)
{

}
delete t1;
delete t2;

return(0);
}
  • Hasil Multithreading C++

multithreading3

Makasih udah nikamatin hidangan BAKSO C# ane dengan cita rasa Multithreading Class Pada C#, JAVA dan C++. Jangan sungkan – sungkan mampir lagi ke RESTOPROGRAM. Masih banyak hidangan – hidangan yang akan memanjakan agan – agan semua.

Jangan lupa share hidangan ane di media sosial yang ada di bawah ini !!!