10. Multithreading

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

10. Multithreading. Java. public class SumTest { public static void main(String a1[]) { int a, b, sum; a = Integer.parseInt (a1[0]); b = Integer.parseInt (a1[1]); sum = a + b ; // 두 수를 더하는 부분입니다 System.out.println (" 두수의 합은 " + sum + " 입니다 "); } - PowerPoint PPT Presentation

Text of 10. Multithreading

PowerPoint Presentation

10. Multithreading1public class SumTest { public static void main(String a1[]) { int a, b, sum; a = Integer.parseInt(a1[0]); b = Integer.parseInt(a1[1]); sum = a + b ; // System.out.println(" " + sum + ""); }}Java12Multiple threads on multiple CPUsMultiple threads sharing a single CPU

Threads Concept

3Thread Classjava.lang PackageThread()Thread(String s)Thread(Runnable r)Thread(Runnable r, String s)10. 3 4MethodMethodDescriptionstatic void sleep(long msec)throws InterruptedExceptionCauses the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.static void sleep(long msec, int nsec) throws InterruptedExceptionCauses the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.String getName()Returns this thread's name.void setName(String s)Changes the name of this thread to be equal to the argument name.void start()Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.final int getPriority()Returns this thread's priority.final void setPriority(int p)Changes the priority of this thread.void join() throws InterruptedExceptionWaits for this thread to die.void run()If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.void suspend()This method has been deprecated, as it is inherently deadlock-pronevoid resume()This method exists solely for use with suspend(), which has been deprecated because it is deadlock-prone10. 4 5StateMachine diagram

10. 5 6Thread statesNewRunnable(ready)Running(executing)WaitingDead(finished)10. 67Thread Classclass ThreadA extends Thread { ......... public void run() { .... } .......}ThreadA ta = new ThreadA();ta.start();10. 78Runnable Interfaceclass RunnableB extends Applet implements Runnable { ......... public void run() { ........ } ........}public interface Runnable { public void run();}10. 89ExampleRunnableB rb = new RunnableB(); Thread tb = new Thread(rb);tb.start();RunnableB rb = new RunnableB();new Thread(rb).start(); 10. 910ExercisesThreadFromThread.java01020304050607080910111213class ThreadTest extends Thread { public void run() { for (int i=1 ; i