.


:




:

































 

 

 

 


. Thread.




 

 

, thread stream . thread , stream. , , thread, .

, Java.

, ;

, ;

, ;

-.

, .

, Object (. 9):

 

 

 
 

 


9

java.lang.Thread. , .

, .

-, - Thread. :

public class MyClass extends Thread.

-, Runnable. , , , Frame, , Applet. Java , Runnable .

public class MyClass extends Frame implements Runnable.

, run().

Thread. , - ', run(). Runnable run() , (this).

, run(). , : Windows run() . run() start() ( !).

 

 

  1. . Runnable. .

 

ϳ , : , , .. , Thread.

. start():

public void start();

' Thread ' , Runnable, run(), .

run() , . , run() , , ( ) . , , :

public final boolean isAlive();

. , ' stop():

public final void stop();

. sleep() ( ):

public static void sleep(long ms);

. . suspend() :

public final void suspend();

resume():

public final void resume();

. Thread . .

1) , , 䳿:

2) , Runnable.

3) Thread .

4) start() . , , . start()Thread ( new) start() ᒺ:

myThread.start()

start()

MyClass.start()

main(). .

5) run() . run() . ( repaint()) ( sleep()), .

6) stop().

, . , Thread Runnable. run(). ' , , Runnable .

 

  1. . Throwable, Exception, Error. 29. .

(exception) , . () Java- , , . , , FileNotFoundException, (throw) - . 쳭 : , , .

ϳ Java- ᒺ-, ᒺ . ᒺ, , ( , ). ᒺ , , .

. try-catch . , - IOException:

try {

System.in.read(buffer, 0, 255);

...

}

catch (IOException e) {

String err = e.toString();

System.out.println(err);

}

, Java try catch, . , try , catch .

. catch , ᒺ-. IOException, e. ᒺ Java ᒺ. eIOException. ᒺ, , . ( toString()).

, ? , . . , , , , , ( , , , . , ).

, , , , . , , , . . ., .

: , .

, . , actionPerformed() :

public void actionPerformed(ActionEvent evt)

{

String arg = evt.getActionCommand();

if ("Check".equals(arg))

{

String str1 = textField1.getText();

String str2 = textField2.getText();

int first = Integer.parseInt(str1);

int second = Integer.parseInt(str2);

String answer = "No";

if (first*first==second || second*second==first)

{ answer = "Yes"; }

displayStr = "Check Square - " + answer;

repaint();

}

}

, . , Check, . NumberFormatException, Java. , :

public void actionPerformed(ActionEvent evt)

{

String arg = evt.getActionCommand();

if ("Check".equals(arg))

{

String str1 = textField1.getText();

String str2 = textField2.getText();

try {

int first = Integer.parseInt(str1);

int second = Integer.parseInt(str2);

String answer = "No";

if (first*first==second || second*second==first)

answer = "Yes";

displayStr = "Check Square - " + answer;

}

catch (NumberFormatException e) {

displayStr = Input format error;

}

repaint();

}

}

, ( ).

first=first/second;

( , ), , ArithmeticException. :

public void actionPerformed(ActionEvent evt)

{

String arg = evt.getActionCommand();

if ("Check".equals(arg))

{

String str1 = textField1.getText();

String str2 = textField2.getText();

try {

int first = Integer.parseInt(str1);

int second = Integer.parseInt(str2);

String answer = "No";

first = first/second;

if (first*first==second || second*second==first)

answer = "Yes";

displayStr = "Check Square - " + answer;

}

catch (NumberFormatException e) {

displayStr = Input format error;

}

catch (ArithmeticException e) {

displayStr = Division by zero

}

repaint();

}

}

catch , try.

, . :

public int fact(int num) throws IllegalArgumentException

{

if (num < 0 || num>10)

{

throw new IllegalArgumentException(Number out of range);

}

int res=1;

for (int i =1; i<=num; i++)

res*= i;

return res;

}

fact() , , fact():

try {

f = fact(x);

...

}

catch (IllegalArgumentException e) {

displayStr=e.toString();

}

, [2].

ϳ , , : (, IOException, ArithmeticException) throw: , throw new IllegalArgumentException. - try-catch.

 

 





:


: 2016-07-29; !; : 746 |


:

:

- - , .
==> ...

1701 - | 1646 -


© 2015-2024 lektsii.org - -

: 0.03 .