เทรด (Threads)
Home  Contents KMArticlesMembersSponsorsAbout us

ปรับปรุง : 2556-06-20 (ปรับ template)
ขอบซ้ายขอบบน
เทรด (Threads)
ข้อมูลจาก : http://www.sun.com/training/catalog/courses/CX-310-035.xml
Section 7: Threads
Write code to define, instantiate and start new threads using both java.lang.Thread and java.lang.Runnable.
Recognize conditions that might prevent a thread from executing.
Write code using synchronized wait, notify and notifyAll to protect against concurrent access problems and to communicate between threads.
Define the interaction among threads and object locks when executing synchronized wait, notify or notifyAll.
Rule
http://www.janeg.ca/scjp/threads/overview.html
- on an operating system a running program is known as a process 
- a single process can have seperate runnable tasks called threads 
- a thread is a single sequential flow of control within a process 
- a thread is also referred to as a lightweight process 
- with a single-processor CPU, only one thread is executing at any given time 
- the CPU quickly switches between active threads giving the illusion that they are all executing at the same time (logical concurrency) 
- on multi-processor systems several threads are actually executing at the same time (physical concurrency) 
- multi-programming occurs when multiple programs or processes are executed 
- multi-threading occurs when concurrency exists amoung threads running in a single process (also referred to as multi-tasking) 
- Java provides support for multi-threading as a part of the language 
- support centers on the: 

- java.lang.Thread class
- java.lang.Runnable interface
- java.lang.Object methods wait(), notify(), and notifyAll
- synchronized keyword - every Java program has at least one thread which is executed when main() is invoked - all user-level threads are explicitly constructed and started from the main thread or by a thread originally started from main() - when the last user thread completes any daemon threads are stopped and the application stops - a thread's default daemon status is the same as that of thread creating it - you can check the daemon status using isDaemon() - you can set the daemon status using setDaemon().
ตัวอย่าง 1. แสดงอักษรใน command line วินาทีละตัว
:: ต้องมี try .. catch จับ InterruptedException
:: ใช้ throws InterruptedException แทน catch ได้
public class slownum1 {
public static void main(String a[]) { 
  int cnt = 0;
  while (true) {
    try {
      Thread.currentThread().sleep(1000);
      cnt++;
    } catch (InterruptedException e) { }
    System.out.println(cnt);
  }
} 
}
import java.io.*; public class slownum2 { public static void main(String a[]) throws InterruptedException { int cnt = 0; while (true) { Thread.currentThread().sleep(1000); cnt++; System.out.println(cnt); } } }

ตัวอย่าง 2. โปรแกรมแสดงการทำงานของ Thread, Runable และ sleep
/* 
DOS>javac numrun.java
DOS>appletviewer numrun.java
<applet code="numrun.class" width=99 height=200></applet> 
*/
import java.awt.*;
import java.applet.*;
public class numrun extends Applet implements Runnable {
  Thread timer;
  int cnt;         
  public void paint(Graphics g) { 
    g.setColor(Color.white);
    g.drawString(Integer.toString(cnt),1,cnt); 
  }
  public void start() {
    setBackground(Color.black);
    timer = new Thread(this);
    timer.start();
  }
  public void run() {
    Thread me = Thread.currentThread();
    while (timer == me) {
      try {
        Thread.currentThread().sleep(100);
        cnt++;
      } catch (InterruptedException e) { }
      repaint();
    }
  } 
}
"Imagination is more important than knowledge" - Albert Einstein
Home
Thaiabc.com
Thainame.net
Lampang.net
Nation university
PHP
MySQL
Visual basic.NET
TabletPC
Linux
Online quiz
Download
Search engine
Web ranking
Add website
Blog : Education
Blog : ACLA
Blog : Lampang
Facebook.com
Twitter.com
About us
My dream
Site map
Sponsor
http://goo.gl/72BPC