Share on Google+Share on Google+

1 Answer(s)      9 years ago
Posted in : Java Beginners


View Answers

May 1, 2008 at 6:47 PM

Hi friend,

public class ConsumerPro extends Thread{
private CubbyHole hole;
private int num;

public ConsumerPro(CubbyHole c, int num) {
hole = c;
this.num = num;
public void run(){
for (int i = 0; i < 10; i++){
System.out.println("Producer #" + this.num + " put: " + i);
sleep((int)(Math.random() * 100));
catch (InterruptedException e) {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Consumer p1 = new Consumer(c, 1);
ConsumerPro c1 = new ConsumerPro(c, 1);


public class CubbyHole {
private int contents;
private boolean available = false;

public synchronized int get() {
while(available == false){
try {
catch(InterruptedException e){
available = false;
return contents;
public synchronized void put(int value) {
while(available == true) {
} catch (InterruptedException e) { }
contents = value;
available = true;

public class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;

public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;

public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
System.out.println("Consumer #" + this.number + " got: " + value);


Related Tutorials/Questions & Answers:
computers - Java Beginners
computers   Describe any 5 methods in any of the library classes of java?  Hi Friend, Please visit the following link: Here you will get lot
How to develope web service with exachanging xml message between computers using SOAP
How to develope web service with exachanging xml message between computers using SOAP  Hi Friends, I want to developed a webservice using soap as a protocol,in this webservice the client sends a set of 15 parameters and program
E Book Witing
Ebook writing eBook Writing As computers are gaining control of the world, the trend of paper books is out and that of eBooks are sweeping
Java UDP
Java UDP       TCP and UDP are transport protocols used for communication between computers. UDP(User Datagram Protocol) is a very simple connectionless protocol. UDP can
EBook Witing
eBook Writing As computers are gaining control of the world, the trend of paper books is out and that of eBooks are sweeping in. Writing and publishing eBooks has become hugely popular amongst writers
Domain Name
The domain name system (DNS) is a series of letters and numbers that are separated by periods and used to name organizations, computers and addresses on the Internet. The domain name is the way that Internet names are located
The Open Source World is a Big Part of PHP
The PHP scripting language is something that can work on many types of computers and can be used alongside HTML. It is also able to work with a variety of different forms of technology. The big reason for this comes from how PHP is an open
Amazon Web Services (AWS)
, magazines, DVDs, videos, electronics, computers, software, apparel &
What is ASCII?
characters in most of the computers that can be displayed on the computer screen... computers. IBM has its own built-in-code called EBCDIC code containing 256
What is Mobile Website
What is Mobile Website Mobile websites are specially designed for viewing mobile contents on Mobile Phones. Earlier websites were seen only on computers... very slower speed. So websites for computers cannot function well on mobile
PHP Is Great for Website Creation
PHP is one of the most commonly used and understood types of website creation scripts in the world. It is being used by all sorts of different computers and is being loaded onto many servers. Here are some reasons why PHP is so great
ASCII values table
ASCII values table          ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII
Java for complete beginners
Java Guide is available at RoseIndia that help beginners to master the language. Because of its usefulness in both software development for computers and application development for mobile phone, Java is in great demand and so
Work Email- Dos and Don?ts
Work Email- Dos and Don’ts       Introduction With the proliferation of computers...; denotes using emails to vent emotions. With the fast pace of computers, it is very
Remote Desktop Setting in Windows 8
In windows 8 operating system you can easily access other computers inside the network by enabling the Remote Desktop Connection in your PC. 1... on the button before it.   5. Mark the 'Allow connections only from computers
EJB, Enterprise java bean, EJB Intoduction- Enterprise Java Beans (EJB) - An Introduction
of the system to be located on separate computers located in different locations... for servicing the clients in other computers. Object-oriented
Flex Mobile Phone Game Development
, iPhone, iPod, smart phones, mobile phones, computers, and on many gazettes
Top 10 Web Development Skills
of mobile devices that surpassed the traditional computers and devices as points
Windows 8 Operating System
Windows 8 is the latest innovation, which has been launched to make easy handling of the Windows operating system. Launched by the Microsoft it is expected to make easier for the use on personal computers and also for home and business
Intranet Application Development, Intranet Application Development Solutions, Intranet Design
Intranet Application Development Intranet Application Development Services India An intranet is a network of private computers linked together and using different web technologies to share the enterprise's
What is E-Commerce Web Design
conducting of business via Internet with the help of computers
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? file location SASLError using PLAIN: not-authorized


Advertisement null