0% found this document useful (0 votes)
3 views2 pages

# NoLockTest - Java

The document contains a Java program that demonstrates a simple producer-consumer model using a shared counter without explicit locking. The Producer increments the counter while the Consumer decrements it, running in separate threads. The program measures and prints the total time taken for the operations and the final count value after execution.

Uploaded by

hoangtu112201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

# NoLockTest - Java

The document contains a Java program that demonstrates a simple producer-consumer model using a shared counter without explicit locking. The Producer increments the counter while the Consumer decrements it, running in separate threads. The program measures and prints the total time taken for the operations and the final count value after execution.

Uploaded by

hoangtu112201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# NoLockTest.

java

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class NoLockTest {

public static void main(String[] args) {


CounterLock c_lock = new CounterLock();
int inc_num = 10001234;
int dec_num = 10000000;

long start = System.currentTimeMillis();


Thread p = new Thread (new Producer(c_lock, inc_num));
p.start();

Thread c = new Thread (new Consumer(c_lock, dec_num));


c.start();
try {
p.join();
} catch (InterruptedException e) {}

try {
c.join();
} catch (InterruptedException e) {}
long finish = System.currentTimeMillis();
System.out.println(inc_num+" inc() calls, "+dec_num+" dec() calls = " +
c_lock.getCount());
System.out.println("No-Lock time: "+(finish-start)+"ms");
}
}

class Producer implements Runnable{

private CounterLock myCounter;


int num;

public Producer(CounterLock x, int Num) {


this.num=Num;
this.myCounter = x;
}

@Override
public void run() {
for (int i = 0; i < num; i++) {
myCounter.inc();
}
}
}

class Consumer implements Runnable{

private CounterLock myCounter;


int num;

public Consumer(CounterLock x, int Num) {


this.num=Num;
this.myCounter = x;
}
@Override
public void run() {
for (int i = 0; i < num; i++) {
myCounter.dec();
}
}
}

class CounterLock {

private long count = 0;

public void inc() {


this.count++;
}

public void dec() {


this.count--;
}

public long getCount() {


return this.count;
}
}
Beta
0 / 0
used queries
1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy