Blog

[Problem] Printing Even and Odd using Two Threads with Coordination

Problem

Create two threads - one that prints odd numbers till 10 in the format "Odd 1", "Odd 3" etc. and one that prints even numbers till 10 in the format "Even 0", "Even 2" etc..

Need to implement coordination between them so that the output will be:

Even0

Odd1

Even2

Odd3

Even4

Odd5

Even6

Odd7

Even8

Odd9

Even10

Wait, Notify and NotifyAll Methods of the Object Class

Java.lang.Object provides three methods – notify(), notifyAll() and wait () – to improve the efficiency communication between threads. You will need to understand the synchronization process in Java to understand the communication using wait, notify and notifyAll.

  • obj.wait()

    • makes a thread wait on an object (obj) until it receives a notification from a notify() or notifyAll() on the same object.

Creating and Executing Threads in Java

You can create threads directly instantiating a Thread class or using the newer better and safer concurrency package features such as Executors.

Though it is not preferred to create threads directly in new code, learning to create threads directly working with Thread class helps to understand basics well.

We can create a Thread by

  • extending the Thread class or

  • implementing Runnable interface and passing it to a Thread class constructor.

Daemon Threads in Java

A daemon thread is a background thread and dies when the thread that created it ends.

A thread that is not daemon is called a user thread.

We can make a thread as daemon thread by calling setDaemon (true) on the thread instance.

We can call setDaemon() for a thread only before it starts, else an IllegalThreadStateException will be thrown.

 

Example:

MyThread t1= new MyThread ();

t1.setDaemon(true);

t1.start(); 

Web Services Support in Java - JAX-WS and JAX-RS

Java supports both SOAP-based and REST-style web services.

 

JAX-WS

  • Java API for XML Web services (JAX-WS) can be used for SOAP-based as well as REST-style, but more suited for SOAP.

    • The current version of JAVA–WS is 2.x and previous version was called JAX RPC 1.x.

    • JAX–WS is commonly shortened to JWS for Java Web Services.

Pages

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)