Примеры

Пример по теме “Java. Синхронизация”

class Callme {
	synchronized void call(String msg){
		System.out.print("["+msg);
		try{Thread.sleep(1000);
		}catch(Exception e){}
		System.out.print("]");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);
	}
 
}
public class Sync {
	public static void main(String a[]){
		Callme target = new Callme();
		new Caller(target, "Hello");
		new Caller(target, "Synchronized");
		new Caller(target, "World");
	}
 
}

Пример по теме “Java. Взаимодействие процессов”. Producer and consumer

// An implementation of a producer and consumer
class Q {int n;

 

synchronized int get() {

try{Thread.sleep(1000);}

catch(Exception e){}

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

return n;

}

 

 

 

 

 

 

 

boolean valueSet = false; 

 

synchronized int get() {

    if(!valueSet)

      try {

        wait();

      } catch(InterruptedException e) {

        System.out.println("interruptedException caught");

      }

 

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

      valueSet = false;

      notify();

      return n;

}

  synchronized void put(int n) {this.n = n;

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

}

}

 

  synchronized void put(int n) {    if(valueSet)

      try {

        wait();

      } catch(InterruptedException e) {

        System.out.println("interruptedException caught");

      }

 

      this.n = n;

      valueSet = true;

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

      notify();

  }

}

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);

 

System.out.println("Press Control-C to stop.");

}

}

 

Пример по теме “Java. Нити процессов”. Thread priorities

// Demonstrate thread priorities.

class clicker implements Runnable {

int click = 0;
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 (InterruptedException e) {
System.out.println("Main thread interrupted.");
}

lo.stop();
hi.stop();

// Wait for child threads to terminate.
try {
hi.t.join();
lo.t.join();
} catch (InterruptedException e) {

System.out.println("InterruptedException caught");
}
System.out.println("Low-priority thread: " + lo.click);
System.out.println("High-priority thread: " + hi.click);
}
}

Пример по теме “Java. Нити процессов”. Runnable interface

Runnable interface

// Create a second thread.

class NewThread implements Runnable {

Thread t;

 

NewThread() {

// Create a new, second thread

t = new Thread(this, "Demo Thread");

System.out.println("Child thread: " + t);

t.start(); // Start the thread

}

 

// This is the entry point for the second thread.

public void run() {//2-ой процесс

try {

for(int i = 5; i > 0; i--) {

System.out.println("Child Thread: " + i);

Thread.sleep(500);

}

} catch (InterruptedException e) {

System.out.println("Child interrupted.");

}

System.out.println("Exiting child thread.");

}

}

 

class ThreadDemo {

public static void main(String args[]) {//1-ый процесс

new NewThread(); // create a new thread

 

try {

for(int i = 5; i > 0; i--) {

System.out.println("Main Thread: " + i);

Thread.sleep(1000);

}

} catch (InterruptedException e) {

System.out.println("Main thread interrupted.");

}

System.out.println("Main thread exiting.");

}

}

 

Translate Переводчик

Подписка на новости

SmartResponder.ru
Ваш e-mail: *
Ваше имя: *

Хостинг для Wordpress сайтов