.


:




:

































 

 

 

 


(contrained properties)




, JavaBeans (contrained properties). , , . .. . , PropertyVetoException. set - throws PropertyVetoException, set -. , , , .

. , get - set -. set - PropertyVetoException

public void <PropertyName> ( param) throws PropertyVetoException.

/ .

addPropertyChangeListener()

RemovePropertyChangeListener()

addVetoableChangeListener(VetoableChangeListener v) removeVetoableChangeListener(VetoableChangeListener v). VetoableChangeListener

void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException().

PropertyChangeSupport, , java.beans VetoableChangeSupport. , .

SomeBean, . someProperty() . .

/* # 26: bean- : SomeBean.java */

import java.beans.*;

public class SomeBean {

private String someProperty = null;

private VetoableChangeSupport vcs;

public SomeBean(){

vcs = new VetoableChangeSupport(this);

}

public void addVetoableChangeListener

(VetoableChangeListener pcl){

vcs.addVetoableChangeListener(pcl);

}

public void removeVetoableChangeListener

(VetoableChangeListener pcl){

pcs.removePropertyChangeListener(pcl);

}

 

public String getSomeProperty(){

return someProperty;

}

public void setSomeProperty(String value) throws

PropertyVetoException{

vcs.fireVetoableChange(someProperty, someProperty, value);

someProperty = value;

}

}

, . PropertyChangeSupport VetoableChangeSupport set - throws PropertyVetoException. someProperty , .

. , , / . .

ActionEvent, .

13

1. . JTextField, JButton JLabel. , .

2. JPanel . ; . BorderLayout.

3. 2 , , .

4. 2 , , , . ,
ArrayList.

5. 2 . . , TreeSet.

6. JLabel, : ; , JTextField, , (, 0.1) . x=0. , . , .

7. , , ..

8. , GIF-, .

9. (, , ). . .

10. . 1 9.

11. . , , , . .

12. ( ). . .

13. / . ,
/.

14. 12 , .

15. . , .

16. ( ) / /.

B

4 , , , .

13

13.1.

?

1) FlowLayout;

2) GridLayout;

3) BorderLayout;

4) CardLayout.

13.2.

:

import java.awt.*;

public class Quest2 extends Frame{

Quest2(){

Button yes = new Button("YES");

Button no = new Button("NO");

add(yes);

add(no);

setSize(100, 100);

setVisible(true);

}

public static void main(String[] args){

Quest2 q = new Quest2();

} }

:

1) , , YES NO ;

2) YES, ;

3) NO, ;

4) YES NO.

13.3.

FlowLayout?

1) FlowLayout.RIGHT;

2) FlowLayout.LEFT;

3) FlowLayout.CENTER;

4) FlowLayout.LEADING;

5) .

13.4.

?

import java.awt.*; public class Quest4 extends java.applet.Applet{ Button b = new Button("YES"); public void init(){ add(b); add(b); add(new Button("NO")); add(new Button("NO")); }}

1) YES NO;

2) YES NO;

3) YES NO;

4) YES NO.

13.5.

JheckBox :

JCheckBox ob = new JCheckBox();

?

1) ob.addItemListener();

2) ob.addItemListener(this);

3) addItemListener(this);

4) addItemListener();

5) .

 
14

Thread Runnable

(Rich Client)
Web- (Thin Client) , , . , , . , , ( ) . : Thread Runnable.

// # 1: Thread: Talk.java

package chapt14;

public class Talk extends Thread {

public void run() {

for (int i = 0; i < 8; i++) {

System. out. println("Talking");

try {

// 400

Thread. sleep (400);

} catch (InterruptedException e) {

System. err. print(e);

}

}

}

}

Runnable run(). :

/* # 2: Runnable: Walk.java: WalkTalk.java */

package chapt14;

 

public class Walk implements Runnable {

public void run() {

for (int i = 0; i < 8; i++) {

System. out. println("Walking");

try {

Thread. sleep (400);

} catch (InterruptedException e) {

System. err. println(e);

}

}

}

}

package chapt14;

 

public class WalkTalk {

public static void main(String[] args) {

//

Talk talk = new Talk();

Thread walk = new Thread(new Walk());

//

talk.start();

walk.start();

 

//Walk w = new Walk(); // ,

// w.run(); // , !

}

}

Talk Walk
: Talking Walking. , , .


Thread : , , . (NEW) . (RUNNABLE) start(), run() .

. 14.1.

, Thread.State:

NEW , ;

RUNNABLE ;

BLOCKED ;

WAITING ;

TIMED_WAITING ;

TERMINATED .

getState().

(WAITING) wait(), suspend() (deprecated-) /, . ( ) (TIMED_WAITING) sleep(long millis) wait(long timeout), InterruptedException. suspend() resume() (deprecated-), wait() notify() notifyAll(). (TERMINATED), interrupt(), stop() (deprecated-) run() . , , . interrupt() , . , .

Runnable start(), run(). , Walk, Thread Walk . run() , .

suspend(), resume() stop() deprecated- , -
.

1 ( MIN_PRIORITY) 10 (MAX_PRIORITY) setPriority(int prior). getPriority().

// # 3: : PriorityRunner.java: PriorThread.java

package chapt14;

public class PriorThread extends Thread {

public PriorThread(String name){

super (name);

}

public void run(){

for (int i = 0; i < 71; i++){

System. out. println(getName() + " " + i);

try {

sleep (1); // sleep(0);

} catch (InterruptedException e) {

System. err. print("Error" + e);

}

}

}

}

package chapt14;

 

public class PriorityRunner {

public static void main(String[] args) {

PriorThread min = new PriorThread("Min"); //1

PriorThread max = new PriorThread("Max"); //10

PriorThread norm = new PriorThread("Norm"); //5

min.setPriority(Thread.MIN_PRIORITY);

max.setPriority(Thread.MAX_PRIORITY);

norm.setPriority(Thread.NORM_PRIORITY);

min.start();

norm.start();

max.start();

}

}

, , .

. .

ThreadGroup tg = new ThreadGroup(" 1");

Thread t0 = new Thread(tg, " 0");

, , . , ,
getThreadGroup(). , . , , .

() sleep( ) Thread. yield(), . join() , , , .

// # 4: : JoinRunner.java

package chapt14;

class Th extends Thread {

public Th(String str) {

super ();

setName(str);

}

public void run() {

String nameT = getName();

System. out. println(" " + nameT);

if ("First".equals(nameT)) {

try {

sleep (5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System. out. println(" "

+ nameT);

} else if ("Second".equals(nameT)) {

try {

sleep (1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System. out. println(" "

+ nameT);

}

}

}

public class JoinRunner {

public static void main(String[] args) {

Th tr1 = new Th("First");

Th tr2 = new Th("Second");

tr1.start();

tr2.start();

try {

tr1.join();

System. out. println(" main");

} catch (InterruptedException e){

e.printStackTrace();

}

/* join() main tr1 */

}

}

, :

First

Second

Second

First

main

join() tr1, tr2 , main, tr1.

yield() , . , .

// # 5: : YieldRunner.java

package chapt14;

public class YieldRunner {

public static void main(String[] args) {

new Thread() {

public void run() {

System. out. println(" 1");

Thread. yield ();

System. out. println(" 1");

}

}.start();

new Thread() {

public void run() {

System. out. println(" 2");

System. out. println(" 2");

}

}.start();

}

}

:

1

2

2

1

yield() run() , , , , .

-

- , . - , -. setDaemon(boolean value), , -. boolean isDaemon() , .

/* # 6: -: DemoDaemonThread.java */

package chapt14;

class T extends Thread {

public void run() {

try {

if (isDaemon()){

System. out. println(" -");

sleep (10000); // 1

} else {

System. out. println(" ");

}

} catch (InterruptedException e) {

System. err. print("Error" + e);

} finally {

if (!isDaemon())

System. out. println(

" ");

Else

System. out. println(

" -");

}

}

}

package chapt14;

 

public class DemoDaemonThread {

public static void main(String[] args) {

T usual = new T();

T daemon = new T();

daemon.setDaemon(true);

daemon.start();

usual.start();

System. out. println(

" main");

}

}

, , :

main

-

- (- sleep(10000)) , main(). - - ( ) main(), , - . -, .

. , , , , .
repaint() , . paint() .

/* # 7: : GraphicThreadsDemo.java */

package chapt14;

import java.awt.Color;

import java.awt.Container;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

 

public class GraphicThreadsDemo extends JFrame {

JPanel panel = new JPanel();

Graphics g;

JButton btn = new JButton(" ");

int i;

 

public GraphicThreadsDemo() {

setBounds(100, 200, 270, 350);

Container contentPane = getContentPane();

contentPane.setLayout(null);

btn.setBounds(50, 10, 160, 20);

contentPane.add(btn);

panel.setBounds(30, 40, 200, 200);

panel.setBackground(Color.WHITE);

contentPane.add(panel);

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ev) {

new BallThread(panel).start();

i++;

repaint();

}

});

}

public static void main(String[] args) {

GraphicThreadsDemo frame =

new GraphicThreadsDemo();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

public void paint(Graphics g){

super. paint(g);

g.drawString(" : " + i, 65, 300);

}

}

class BallThread extends Thread {

JPanel panel;

private int posX, posY;

private final int BALL_SIZE = 10;

private double alpha;

private int SPEED = 4;

 

BallThread(JPanel p) {

this. panel = p;

//

posX = (int)((panel.getWidth() - BALL_SIZE)

* Math.random());

posY = (int)((panel.getHeight() - BALL_SIZE)

* Math.random());

alpha = Math.random() * 10;

}

public void run() {

while (true) {

posX += (int)(SPEED * Math.cos(alpha));

posY += (int)(SPEED * Math.sin(alpha));

//

if (posX >= panel.getWidth() - BALL_SIZE)

alpha = alpha + Math.PI - 2 * alpha;

else if (posX <= 0)

alpha = Math.PI - alpha;

if (posY >= panel.getHeight() - BALL_SIZE)

alpha = -alpha;

else if (posY <= 0)

alpha = -alpha;

paint(panel.getGraphics());

}

}

public void paint(Graphics g) {

//

g.setColor(Color.BLACK);

g.fillArc(posX, posY, BALL_SIZE, BALL_SIZE, 0, 360);

g.setColor(Color.WHITE);

g.drawArc(posX + 1, posY + 1, BALL_SIZE,

BALL_SIZE, 120, 30);

try {

sleep(30);

} catch (InterruptedException e) {

e.printStackTrace();

}

//

g.setColor(panel.getBackground());

g.fillArc(posX, posY, BALL_SIZE, BALL_SIZE, 0, 360);

}

}

.14.2.

stop() , null . start() .

synchronized

, , , ; , . , //. synchronized. /, 32 .

. main() a SynchroThreads . Synchro, FileWriter, . Synchro . writing() Synchro. Synchro. synchronized. , . , . wait() .

.

/* # 8: : MyThread.java: Synchro.java: SynchroThreads.java */

package chapt14;

import java.io.*;

 

public class Synchro {

private FileWriter fileWriter;

 

public Synchro(String file) throws IOException {

fileWriter = new FileWriter(file, true);

}

public void close() {

try {

fileWriter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

public synchronized void writing(String str, int i) {

try {

System. out. print(str + i);

fileWriter.append(str + i);

Thread. sleep ((long)(Math. random () * 50));

System. out. print("->" + i + " ");

fileWriter.append("->" + i + " ");

} catch (IOException e) {

System. err. print(" ");

e.printStackTrace();

} catch (InterruptedException e) {

System. err. print(" ");

e.printStackTrace();

}

}

}

package chapt14;

 

public class MyThread extends Thread {

private Synchro s;

 

public MyThread(String str, Synchro s) {

super (str);

this. s = s;

}

public void run() {

for (int i = 0; i < 5; i++) {

s.writing(getName(), i);

}

}

}

package chapt14;

import java.io.*;

 

public class SynchroThreads {

public static void main(String[] args) {

try {

Synchro s = new Synchro("c:\\temp\\data.txt");

 

MyThread t1 = new MyThread("First", s);

MyThread t2 = new MyThread("Second", s);

t1.start();

t2.start();

t1.join();

t2.join();

s.close();

} catch (IOException e) {

System. err. print(" ");

e.printStackTrace();

} catch (InterruptedException e) {

System. err. print(" ");

e.printStackTrace();

}

}

}

:

First0->0 Second0->0 First1->1 Second1->1 First2->2

Second2->2 First3->3 Second3->3 First4->4 Second4->4

, writing() , , , .

, , :

First0Second0->0 Second1->0 First1->1 First2->1 Second2->2 First3->3 First4->2 Second3->3 Second4->4 ->4

synchronized

-
, . , synchronized,
. , .

/* # 9: : TwoThread.java */

package chapt14;

public class TwoThread {

public static void main(String args[]) {

final StringBuffer s = new StringBuffer();

new Thread() {

public void run() {

int i = 0;

synchronized (s) {

while (i++ < 3) {

s.append("A");

try {

sleep (100);

} catch (InterruptedException e) {

System. err. print(e);

}

System. out. println(s);

}

} // synchronized

}

}.start();

new Thread() {

public void run() {

int j = 0;

synchronized (s) {

while (j++ < 3) {

s.append("B");

System. out. println(s);

}

} // synchronized

}

}.start();

}

}

, ( ), :

A

AA

AAA

AAAB

AAABB

AAABBB

, , , , .

s, , , .

wait()
notify() synchronized . , .

wait(), , , lock. notify()
notifyAll() . , , , .

/* # 10: wait() notify(): Blocked.java: Runner.java */

package chapt14;

 

public class Blocked {

private int i = 1000;

 

public int getI() {

return i;

}

public void setI(int i) {

this. i = i;

}

public synchronized void doWait() {

try {

System. out. print(" ");

this. wait(); /*

*/

System. out. print(" "); //

Thread. sleep (50);

} catch (InterruptedException e) {

e.printStackTrace();

}

for (int j = 0; j < 5; j++) i/=5;

System. out. print(" ");

}

}

package chapt14;

 

public class Runner {

public static void main(String[] args) {

Blocked lock = new Blocked();

new Thread() {

public void run() {

lock.doWait();

}}.start();

try {

Thread. sleep (500);

} catch (InterruptedException e) {

e.printStackTrace();

}

synchronized (lock) { // 1

lock.setI(lock.getI() + 2);

System. out. print(" ");

lock.notify(); //

}

synchronized (lock) { // 2

lock.setI(lock.getI() + 3);

// doWait()

System. out. print(". ");

try {

lock.wait(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

System. out. print("=" + lock.getI());

}

}

:

. =3

sleep() , . , wait() notify(), :

. =1005

Thread State, , , .

/* # 11: NEW, RUNNABLE, TIMED_WAITING, TERMINATED: ThreadTimedWaitingStateTest.java */

package chapt14;

public class ThreadTimedWaitingStateTest extends Thread {

public void run() {

try {

Thread. sleep (50);

} catch (InterruptedException e) {

System. err. print(" ");

}

}

public static void main(String [] args){

try {

Thread thread = new ThreadTimedWaitingStateTest();

// NEW ,

System. out. println("1: " + thread.getState());

thread.start();

// RUNNABLE

System. out. println("2: " + thread.getState());

Thread.sleep(10);

// TIMED_WAITING

//

System.out.println("3: " + thread.getState());

thread.join();

// TERMINATED

System.out.println("4: " + thread.getState());

} catch (InterruptedException e) {

System.err.print(" ");

}

}

}

:

NEW

RUNNABLE

TIMED_WAITING

TERMINATED

/* # 12: BLOCKED, WAITING: ThreadWaitingStateTest.java */

package chapt14;

public class ThreadWaitingStateTest extends Thread {

public void run() {

try {

synchronized (this) {

wait();

}

} catch (InterruptedException e) {

System.err.print(" ");

}

}

 

public static void main(String[] args) {

try {

Thread thread = new ThreadWaitingStateTest();

thread.start();

synchronized (thread) {

Thread.sleep(10);

// BLOCKED because thread attempting to acquire a lock

System.out.println("1: " + thread.getState());

}

Thread.sleep(10);

// WAITING wait() synchronized

//

System.out.println("2: " + thread.getState());

thread.interrupt();

} catch (InterruptedException e) {

System.err.print(" ");

}

}

}

:

BLOCKED

WAITING

J2SE 5

Java : . : .

1.5 java.util.concurrent.locks, java.util.concurrent.atomic, java.util.concurrent, , , (concurrent) , , , atomic-.

. , , . , , Hashtable, , ( ) . , .

(thread safe) java.util.concurrent. :

ArrayBlockingQueue (FIFO ), PriorityBlockingQueue ( ) ConcurrentLinkedQueue (FIFO );

- ConcurrentHashMap ( Hashtable)
CopyOnWriteArrayList ( List, , );

, Executor, ;

Lock, , , Condition;

(AtomicInteger, AtomicLong,
AtomicReference), SyncronizedInt .;

, Semaphore, CountDownLatch ( ), CyclicBarrier ( , - ) Exchanger ( );

: Thread ( ThreadGroup).

Hashtable. Hashtable , . ConcurrentHashMap , , . ( ), .

( ) , ConcurrentHashMap Hashtable. Linux. .

ConcurrentHashMap Hashtable
  1.00 1.03
  2.59 32.40
  5.58 78.23
  13.21 163.48
  27.58 341.21
  57.27 778.41

// # 13: : Sort.java: ArraySort.java

package chapt14;

import java.util.concurrent.*;

 

public class Sort {

public static final int ITEMS_COUNT = 15;

public static double items [];

// ,

public static Semaphore sortSemaphore =

new Semaphore(0, true);

 

public static void main(String[] args) {

items = new double [ ITEMS_COUNT ];

for (int i = 0; i < items.length; ++i)

items [i] = Math. random ();

new Thread(new ArraySort(items)).start();

for (int i = 0; i < items. length; ++i) {

/*

*

*

*

*/

sortSemaphore. acquireUninterruptibly();

System. out. println(items [i]);

}

}

}

class ArraySort implements Runnable {

private double items[];

 

public ArraySort(double items[]) {

this. items = items;

}

public void run(){

for (int i = 0; i < items.length - 1; ++i) {

for (int j = i + 1; j < items.length; ++j) {

if (items[i] < items[j]) {

double tmp = items[i];

items[i] = items[j];

items[j] = tmp;

}

}

//

Sort. sortSemaphore. release();

try {

Thread. sleep (71);

} catch (InterruptedException e) {

System. err. print(e);

}

}

Sort. sortSemaphore. release();

}

}

14

1. C : , .

2. C : . .

3. , , , .

4. , 3.

5. , . .

6. , ( ) . , () y, , .. . .

7. ,
. .

8. , .

9. C , .
.

10. .
( , ).

11. .

B

4 (). .

14

14.1.

:

class Q implements Runnable{

int i = 0;

public int run(){

System.out.println("i = "+ ++i);

return i;

}}

public class Quest1 {

public static void main(String[] args) {

Q ob = new Q();

ob.run();

}}

:

1) i = 0;

2) i = 1;

3) : Thread ;

4) : run();

5) : start().

14.2.

:

Thread t1= new Thread();

t1.setPriority(7);

ThreadGroup tg= new ThreadGroup("TG");

tg.setMaxPriority(8);

Thread t2= new Thread(tg,"t2");

System.out.print(" t1="

+ t1.getPriority());

System.out.print(", t2="

+ t2.getPriority());

:

1) t1 = 7, t2 = 5;

2) t1 = 7, t2 = 8;

3) t1 = 10, t2 = 8;

4) .

14.3.

:

class T1 implements Runnable{

public void run(){

System.out.print("t1 ");

} }

class T2 extends Thread{

public void run(){

System.out.print("t2 ");

} }

public class Quest3 {

public static void main(String[] args) {

T1 t1 = new T1();

T2 t2 = new T2(t1);

t1.start();

t2.start();

} }

:

1) t1 t2;

2) t2 t1;

3) : start() T1;

4) : T2 , Thread;

5) .

14.4.

, ? ( )

1) sleep() ;

2) stop();

3) run();

4) notifyAll();

5) wait() null.

14.5.

:

class Quest5 extends Thread {Quest5 () { }Quest5 (Runnable r) { super(r); } public void run() { System.out.print("thread "); } public static void main(String[] args) { Runnable r = new Quest5(); //1Quest5 t = new Quest5(r); //2 t.start(); } }

:

1) //1;2) //2;3) thread;4) thread thread; 5) , .

 
15

Java . java.net. . . Internet-, Web-, e-mail, , . , , TCP/IP, UDP.

/ , -, -. - , . . , . . , .

TCP/IP :

HTTP - Hypertext Transfer Protocol (WWW);

NNTP - Network News Transfer Protocol ( );

SMTP - Simple Mail Transfer Protocol ( );

POP3 - Post Office Protocol ( );

FTP - File Transfer Protocol ( ).

TCP/IP IP-, . 32- , , , 0 255. IP- , . IP- . Internet IP- (: www.bsu.by), . DNS (Domain Name Server), , IP-. , IP- 127.0.0.1 localhost. IP- , 217.21.43.10:443. 443. 1 1024 , , , : 20 FTP -, 21 FTP -, 53 DNS, 80 HTTP, 25 SMTP, 110 POP3, 119 NNTP. . .

Internet URL. URL (Universal Resource Locator) (http, https, ftp ..) URI (Universal Resource Identifier). URI Internet-, , , :

http://www.bsu.by

URI , , , . . : %20 . : & , ? , + , # (_#_).

IP- InetAddress java.net.

InetAddress public -. .





:


: 2016-04-03; !; : 675 |


:

:

.
==> ...

1759 - | 1614 -


© 2015-2024 lektsii.org - -

: 0.753 .