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.
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.
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
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
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