신호량이 다선정 통신에서 운용되다
블록 동기화 또는 동기화 방법에서 사용해야 합니다.
class CommonTalkVar{
private char product;
/**
* true:
* false:
* */
private boolean isProduced=false;
/**
* @description production method
*/
public synchronized void putCommonData(char product){
if(isProduced){
try {
System.out.println(Thread.currentThread().getName()+" ");
wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
this.product=product;
this.isProduced=true;
System.out.println(Thread.currentThread().getName()+" :"+this.product);
notify();
}
/**
* @description consumer method
*/
public synchronized char getCommonData(){
if(!isProduced){
try {
System.out.println(Thread.currentThread().getName()+" ");
wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
this.isProduced=false;
System.out.println(Thread.currentThread().getName()+" :"+this.product);
notify();
return this.product;
}
}
class Consumer extends Thread{
private CommonTalkVar commonTalkVar;
public Consumer(CommonTalkVar commonTalkVar){
this.commonTalkVar=commonTalkVar;
}
@Override
public void run() {
char tempc;
do{
try {
Thread.sleep((int)(Math.random()*3000));
}catch (InterruptedException e){
e.printStackTrace();
}
tempc=commonTalkVar.getCommonData();
}while (tempc!='D');
}
}
class Product extends Thread{
private CommonTalkVar commonTalkVar;
public Product(CommonTalkVar commonTalkVar){
this.commonTalkVar=commonTalkVar;
}
@Override
public void run() {
for (char i = 'A'; i <= 'D'; i++){
try {
Thread.sleep((int)(Math.random()*3000));
}catch (InterruptedException e){
e.printStackTrace();
}
commonTalkVar.putCommonData(i);
}
}
}
public class ConsumerAndProduct {
public static void main(String[] args) {
CommonTalkVar talkVar = new CommonTalkVar();
new Consumer(talkVar).start();
new Product(talkVar).start();
}
}
다중 노드의 순서 제어
실제로는 notifyAll () 방법의 제어 사용법을 주의하는 것이다
package Thread;
public class Demo1_Synchronized {
/**
* @param args
*
*/
public static void main(String[] args) {
final Printer p = new Printer();
new Thread() {
public void run() {
while(true) {
try {
p.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
try {
p.print2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
try {
p.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class Printer {
Demo d = new Demo();
private static volatile int signal=1;
public void print1() throws InterruptedException {
synchronized (this) {
while(this.signal!=1) {
this.wait();
}
this.signal=2;
System.out.println("11111");
this.notifyAll();
}
}
public void print2() throws InterruptedException {
synchronized (this) {
while(this.signal!=2) {
this.wait();
}
this.signal=3;
System.out.println("22222");
this.notifyAll();
}
}
public void print3() throws InterruptedException {
synchronized (this) {
while(this.signal!=3) {
this.wait();
}
this.signal=1;
System.out.println("33333");
this.notifyAll();
}
}
}
//
class Demo {
}
잠금 동기화
package Thread;
import java.util.concurrent.locks.ReentrantLock;
public class Test6 {
public static void main(String[] args) {
new Ticket12().start();
new Ticket12().start();
new Ticket12().start();
new Ticket12().start();
}
}
class Ticket12 extends Thread {
private static volatile int ticket = 100;
private static final ReentrantLock lock=new ReentrantLock();
public void run() {
while (true) {
lock.lock();
try {
if (ticket <= 0) {
break;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName() + " " + ticket-- + " ");
} finally {
lock.unlock();
}
}
}
}
다시 잠글 수 있는 용법
package Thread;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Demo1_ReentrantLock {
public static void main(String[] args) {
final PrinterReentrantLock p = new PrinterReentrantLock();
new Thread() {
public void run() {
while(true) {
try {
p.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
try {
p.print2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
try {
p.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class PrinterReentrantLock {
private int signal=1;
private static final ReentrantLock lock=new ReentrantLock();
private static final Condition c1=lock.newCondition();
private static final Condition c2=lock.newCondition();
private static final Condition c3=lock.newCondition();
public void print1() throws InterruptedException {
lock.lock();
try {
while (this.signal != 1) {
c1.await();
}
this.signal = 2;
System.out.println("11111");
c2.signal();
} finally {
lock.unlock();
}
}
public void print2() throws InterruptedException {
lock.lock();
try {
while (this.signal != 2) {
c2.await();
}
this.signal = 3;
System.out.println("22222");
c3.signal();
} finally {
lock.unlock();
}
}
public void print3() throws InterruptedException {
lock.lock();
try {
while (this.signal != 3) {
c3.await();
}
this.signal = 1;
System.out.println("33333");
c1.signal();
} finally {
lock.unlock();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.