Java             3GL - .   4GL -     DB-

JAVA

Java

, , (multithreading, light-weight processes) , , .

, . , . , (light-weight processes, threads).

, . , . . . Apple Macintosh, Microsoft Windows, X11/Motif .

, , , , . , , .

Java

Java , . Java , . , , (suspend). (stop), .

1 10 . , , . . . -, . , , . , , , , , .

, . Java .

- , , . Java , wait notify. Java . , , , , .

Thread , . Java- . , , Thread.currentThread. , , , . , .

class CurrentThreadDemo {
public static void main(String args[]) { 
Thread t = Thread.currentThread();
t.setName("My Thread");
System.out. println("current thread: " + t);
try {
for (int n = 5; n > 0; n--) {
System.out.println(" " + n);
Thread.sleep(1000);
} }
catch (InterruptedException e) {
System.out.println("interrupted");
}
} }

t. setName, My Thread, , . , 5, Thread.sleep() 1 . , 10 . try/catch . , Thread.sleep() InterruptedException. , - . . :

:\> java CurrentThreadDemo
current thread: Thread[My Thread,5,main]
5
4
3
2
1

, Thread My Thread. 5 , , main , .

Runnable

, ? Thread. Thread , . , Runnable. , , run. , .

class ThreadDemo implements Runnable {
ThreadDemo() {
Thread ct = Thread.currentThread();
System.out.println("currentThread: " + ct);
Thread t = new Thread(this, "Demo Thread");
System.out.println("Thread created: " + t);
t.start();
try {
Thread.sleep(3000);
} 
catch (InterruptedException e) { 
System.out.println("interrupted");
}
System.out.println("exiting main thread");
}
public void run() { 
try {
for (int i = 5; i > 0; i--) { 
System.out.println("" + i);
Thread.sleep(1000);
} } 
catch (InterruptedException e) {
System.out.println("child interrupted");
} 
System.out.println("exiting child thread");
}
public static void main(String args[]) { 
new ThreadDemo();
} }

, run , , . main new Thread(this, "Demo Thread") Thread, this , run . start, , run. (main) , . Demo Thread - run . , , 5 .

:\> java ThreadDemo

Thread created: Thread[Demo Thread,5,main]

5

4

3

exiting main thread

2

1

exiting child thread

Java , , . , . setPriority Thread. NORM_PRIORITY, , . , , . 10 . , -. , while true , , .

class Clicker implements Runnable {
int click = 0;
private Thread t;
private boolean running = true;
public clicker(int p) {
t = new Thread(this);
t.setPriority(p);
} 
public void run() {
while (running) { 
click++;       
} }
public void stop() {
running = false;  }
public void start() {
t.start();
} }
class HiLoPri {
public static void main(String args[]) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
clicker hi = new clicker(Thread.NORM_PRIORITY + 2);
clicker lo = new clicker(Thread.NORM_PRIORITY - 2);
lo.start();
hi.start();
try Thread.sleep(-10000) {
} 
catch (Exception e) {
}
lo.stop();
hi.stop();
System.out.println(lo.click + " vs. " + hi.click); 
} }

, , , 25 :

C:\>java HiLoPri

304300 vs. 4066666

( , ), , . Java , . . , . . - , , . , , - .

Java- , , , , synchronized. , , .

class Callme {
void call(String msg) {
System.out.println("[" + msg);
try Thread.sleep(-1000) {} 
catch(Exception e) {}
System.out.println("]");
} }
class Caller implements Runnable { 
String msg;
Callme target;
public Caller(Callme t, String s) { 
target = t;
msg = s;
new Thread(this).start();
}
public void run() { 
target.call(msg);
} } 
class Synch {
public static void main(String args[]) { 
Callme target = new Callme();
new Caller(target, "Hello.");
new Caller(target, "Synchronized");
new Caller(target, "World");
}
}

, sleep call , 3 - :

[Hello.
[Synchronized
]
[World
] 
]

, , . race condition ( ), , , . , , sleep. , , , , , . ( ), , , synchronized.

Java , wait, notify notifyAll. , final- Object, Java-. . :

wait , - notify .

notify , wait .

notifyAll , wait .

-. : Q, , ; ( Producer), ; ( Consumer), ; , , PC, .

class Q { 

int n;

synchronized int get() {

System.out.println("Got: " + n);

return n;

}

synchronized void put(int n) {

this.n = n;

System.out. println("Put: " + n);

} }

class Producer implements Runnable {

Q q;

Producer(Q q) {

this.q = q;

new Thread(this, "Producer").start();

}

public void run() {

int i = 0;

while (true) {

q.put(i++);

} } }

class Consumer implements Runnable {

Q q;

Consumer(Q q) {

this.q = q;

new Thread(this, "Consumer").start();

}

public void run() {

while (true) {

q.get();

}

} }

class PC {

public static void main(String args[]) {

Q q = new Q();

new Producer(q);

new Consumer(q);

} }

put get Q , , , , , . , :

:\> java PC
Put: 1
Got: 1
Got: 1
Got: 1
Got: 1
Got: 1
Put: 2
Put: 3
Put: 4
Put: 5
Put: 6
Put: 7
Got: 7

, , n 1, 5 . , , , true, false.

Java wait notify . get ( wait), Producer (notify), . , get, Producer ( notify) , . , put, (wait), Consumer , (notify) -. Q.

class Q {
int n;
boolean valueSet = false;
synchronized int get() {
if (!valueSet)
try wait();
catch(InterruptedException e):
System.out.println("Got: " + n);
valueSet = false;
notify();
return n;
}
synchronized void put(int n) {
if (valueSet)
try wait(); catch(InterruptedException e);
this.n = n;
valueSet = true;
System.out.println("Put: " + n);
notify();
} }

, , .

:\> java Pcsynch
Put: 1
Got: 1
Put: 2
Got: 2
Put: 3
Got: 3
Put: 4
Got: 4
Put: 5
Got: 5

(deadlock)

, , . , X, Y, Y, , . Y X, . , , , , .

Thread, .

, Thread.

currentThread

currentThread Thread, .

yield

yield , . , - .

sleep(int n)

sleep n . , , . Java , 10 .

start

start Java, . run . , start .

run

run . Runnable. start , . run, .

stop

stop . , . , stop, , , stop . - -, run , , , .

suspend

suspend stop , , . suspend, , resume.

resume

resume , suspend. , resume , . resume , , , .

setPriority(int p)

setPriority , . Thread -: MIN_PRIORITY, NORM_PRIORITY MAX_PRIORITY, 1, 5 10. NORM_PRIORITY - 1. , , - , MIN_PRIORITY. MAX_PRIORITY . sleep yield, , Java .

SetPriority

1 10.

setName(String name)

setName . . setName , .

getName

getName , setName.

, , ThreadGroup SecurityManager, , Java . , JDK API.

Java , . , , . , Java, , , , Java-, .

Java             3GL - .   4GL -     DB-


,
- ;
- , - ;
- , ;
- , , .




 10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _.
10.11.2021 - 12:36: - Conscience -> . ? - _.
10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _.
10.11.2021 - 12:35: - Ecology -> - _.
10.11.2021 - 12:34: , - War, Politics and Science -> - _.
10.11.2021 - 12:34: , - War, Politics and Science -> . - _.
10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _.
10.11.2021 - 09:18: - New Technologies -> , 5G- - _.
10.11.2021 - 09:18: - Ecology -> - _.
10.11.2021 - 09:16: - Ecology -> - _.
10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _.
10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _.
Bourabai Research -  XXI Bourabai Research Institution