Cara menggunakan php thread example

When you start with multi-threading in PHP, you probably start by creating a Thread class and instantiating one for each of the tasks that you need doing. This is okay when you have just a few tasks, but you can quickly overload your server if you have a lot. Also, even if you scheduled the threads so that they didn't all run at once, it is highly inefficient to spawn a thread per task as there is overhead in creating and destroying the threads. It is much more efficient to re-use threads over and over again to complete these jobs. This is where a "pool" comes in and where one needs to separate "work" (or job/task) from "worker" (the thread).

Below is a script that defines a task class (MyTask) which gets instantiated a number of times with. Each time the task is given a list of numbers to perform calculations on ($inputs). We then instantiate a thread pool and hand the list of tasks to it to work on. The pool of threads will work until all the jobs have been completed, with each thread in the pool working on one job at a time. When all the tasks have been completed we get all the results out by asking the task objects for the results. We don't try to get the data back by going to the pool or the threads.

m_inputs = $inputs; $this->m_outputs = new Threaded(); // we will store the results in here. } public function run() { foreach ($this->m_inputs as $input) { // casting the array to an array is not a mistake // but actually super important for this to work // //github.com/krakjoe/pthreads/issues/813#issuecomment-361955116 $this->m_outputs[] = (array)array( 'property1' => $input * 2, 'property2' => ($input + 2), ); } } # Accessors public function getResults() { return $this->m_outputs; } } function main() { $inputs = range(0,10000); $numInputsPerTask = 20; $inputGroups = array_chunk($inputs, $numInputsPerTask); $numCpus = 4; // I would nomrally dynamically fetch this and sometimes large (e.g. aws C5 instances) $numTasks = count($inputGroups); $numThreads = min($numTasks, $numCpus); // don't need to spawn more threads than tasks. $pool = new Pool($numThreads); $tasks = array(); // collection to hold all the tasks to get the results from afterwards. foreach ($inputGroups as $inputsForTask) { $task = new MyTask($inputsForTask); $tasks[] = $task; $pool->submit($task); } while ($pool->collect()); # We could submit more stuff here, the Pool is still waiting for work to be submitted. $pool->shutdown(); # All tasks should have been completed at this point. Get the results! $results = array(); foreach ($tasks as $task) { $results[] = $task->getResults(); } print "results: " . print_r($results, true); } main();

Getting the Answers Out

When I first started working with threads, I found it was easy enough to create them and get them to output messages, but I was confused as to how to retrieve the results by the main thread when the work had been done. This was because it hadn't clicked in my mind that the "job" stores the information about the parameters of the work and the result, not the thread. The thread just runs the job, with no clue about how it runs. You can think of the thread as a trained monkey that has been taught to turn a crank, and the jobs are containers with cranks on the outside that the monkey can access. The monkey has no clue whats going on inside the container when he turns the crank, but all the containers get their work done and have their result inside. One of the containers may have peeled potatoes when the crank was turned, whereas another might have been a manual generator that charged a battery.

Assalamualaikum wr. wb


Di kesempatan kali ini  ane bakal sedikit ngasih tutorial dasar PHP nih

Yuk mari praktekan .. 

PHP merupakan bahasa pemrograman yang dapat digunakan untuk membuat skrip yang lebih interaktif. Skrip ini kemudian akan diolah dalam web server yang hasilnya dapat dilihat dalam bentuk HTML. 

PHP memungkinkan untuk membuat situs yang lebih interaktif dan lebih mudah untuk dioperasikan. MySQL merupakan bahasa pemrograman database terbuka. Yang mana memungkinkan untuk bisa membuat, merubah, dan mengakses beberapa database sekaligus dalam server. Kombinasi keduanya biasanya digunakan untuk membuat berbagai macam Website.

Sebelum kita lanjut mengenai PHP, sebaiknya agan/sista sudah belajar mengenai dasar-dasar html,css dan mysql ya biar belajar PHP nya jadi lebih mudah. Untuk tipe file / ekstensi nya adalah .php contoh latihan1.php

Apa saja alat tempur yang akan kita gunakan untuk latihan PHP ini ?

Xampp (Jika windows), Mampp (untuk macintosh), dan Lampp(untuk linux, tapi jika di linux bisa kok tanpa Lampp ini nanti deh saya share tutornya install web server tanpa Lampp), dll boleh pake Web server apa saja tapi disini saya menggunakan Xampp ya

Text editor ( Terserah saja apa, notepad biasa juga bisa kok tapi kalo pusing jangan salahin ane ya :v ) kalo ane menggunakan Sublime Text (yg free)

Web browser (bisa Chrome, Mozilla, dll)

Latihan pertama

Kita membuat Echo, echo fungsinya untuk menampilkan teks ke website. Untuk memulai koding php harus diawali dengan <?php dan diakhiri ?> , dan untuk pernyataan php diakhiri dengan ;


Contoh :



Lalu save di dalam folder xampp -> htdocs -> buat folder baru dengan nama latihan -> masuk ke folder latihan. Lalu save dengan nama latihan1.php.

Dan untuk menjalankan latihan1.php, sobat buka web browser dan ketikkan di url localhost/latihan(nama folder nya)/latihan1.php (nama file php nya). Contoh gini :



Berhasil tampil! Jika gagal, pastikan Apache di xampp nya sudah di start ya sob
ane lupa ngasih tau


Menampilkan text di php kita sudah berhasil, sekarang kita coba tambah dan kurang di php. Koding nya seperti ini :




Save dengan nama latihan2.php, lalu sobat buka lagi url nya localhost/latihan/latihan2.php,

dan hasil nya seperti ini :




Selanjutnya kita mainan IF / ELSE di php, jadi permasalahan nya gini :

Saya mau beli nasi goreng
Harga 1 bungkus nasi goreng Rp 10.000
Uang saya hanyar 9000
Gimana caranya biar si php ini ngasih tau kalo uang saya kurang dari harga nasi goreng? Udah belajar > lebih dari, < kurang dari, = sama dengan tapi dalam php conditional statement nya == 2 buah sama dengan. Masih ada conditional statement lain nya seperti && artinya (AND) , || artinya (OR). Nanti kita kasih contoh kok.

Oke kembali ke nasi goreng.

Jadi koding nya adalah seperti ini :




dengan nama latihan3.php, seperti biasa buka latihan3.php nya. Hasilnya :



Berhasil kan
Yuk kita lanjut gimana kalau nilai variable uang dan harga nya kita input kan sendiri? Tanpa kita input lebih dulu di koding nya? Ya begini contoh nya(yang teliti ya) :



Save dengan nama latihan4.php dan silahkan dijalankan. Maka hasilnya,



Sudah bisa kan? Bisa dong! Baiklah sekian dulu belajar PHP dasar nya


Nantikan tulisan tutorial selanjutnya ya, terimakasih 


Seoa bermanfaat 

Waalaikumsalam wr.wb



Apa itu multithreading pada konsep thread?

Multi-threading adalah teknik pemrograman yang memungkinkan beberapa sub proses dalam program dapat berjalan secara paralel. Sebuah thread adalah sebuah bagian program yang dapat berjalan mandiri, sehingga dua atau lebih thread dapat berjalan bersamaan, tanpa yang satu harus menunggu selesainya yang lain.

Dalam pembuatan thread pengguna dibutuhkan pembuatan korespondensi thread pengguna hal ini merupakan kelemahan dari model apa?

Model One to One Kelemahan model ini adalah dalam pembuatan thread pengguna dibutuhkan pembuatan korespondensi thread pengguna. Karena dalam proses pembuatan kernel thread dapat mempengaruhi kinerja dari aplikasi maka kebanyakan dari implementasi model ini membatasi jumlah thread yang didukung oleh sistem.

Apa itu single thread dan multi thread?

Single-Threading adalah sebuah lightweight process (proses sederhana) yang mempunyai thread tunggal yang berfungsi sebagai pengendali/ controller. Multi-Threading adalah proses dengan thread yang banyak dan mengerjakan lebih dari satu tugas dalam satu waktu.

Postingan terbaru

LIHAT SEMUA