CONCURRENT PROGRAMMING
CONCURRENT PROGRAMMING
Java Thread class 3) s ta tic void s l eep() It s l eeps a thread for the s pecified amount of ti me.
• Java provides Thread class to 4) s tatic Thread currentThread() It returns a reference to the currently executing thread object.
It causes the currentl y executing thread object to pause and allow other threads
12) s ta tic void yi el d()
to execute temporarily.
16) voi d des troy() It i s used to destroy the thread group a nd a ll of i ts s ubgroups.
2. public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
3. public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
5. public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
10. public Thread currentThread(): returns the reference of currently executing thread.
11. public int getId(): returns the id of the thread.
12. public Thread.State getState(): returns the state of the thread.
13. public boolean isAlive(): tests if the thread is alive.
14. public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.
15. public void suspend(): is used to suspend the thread(depricated).
16. public void resume(): is used to resume the suspended thread(depricated).
17. public void stop(): is used to stop the thread(depricated).
18. public boolean isDaemon(): tests if the thread is a daemon thread.
19. public void setDaemon(boolean b): marks the thread as daemon or user thread.
20. public void interrupt(): interrupts the thread.
21. public boolean isInterrupted(): tests if the thread has been interrupted.
22. public static boolean interrupted(): tests if the current thread has been interrupted.
• Runnable interface:
• The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable
interface have only one method named run().
1. public void run(): is used to perform action for a thread.
• Starting a thread:
• The start() method of Thread class is used to start a newly created thread. It performs the following tasks:
• A new thread starts(with new callstack).
• The thread moves from New state to the Runnable state.
• When the thread gets a chance to execute, its target run() method will run.
• Java Thread Example by extending Thread class
• FileName: Multi.java
1. class Multi extends Thread{
2. public void run(){
3. System.out.println("thread is running..."); }
4. public static void main(String args[]){
5. Multi t1=new Multi();
6. t1.start(); } }
• Output:
• thread is running...
Java Thread Example by implementing Runnable interface
FileName: Multi3.java
1.class Multi3 implements Runnable{
2.public void run(){
3.System.out.println("thread is running...");
4.}
5.
6.public static void main(String args[]){
7.Multi3 m1=new Multi3();
8.Thread t1 =new Thread(m1); // Using the constructor Thread(Runnable r)
9.t1.start();
10. }
11.}
Output:
thread is running...
• Declarative Programming Paradigm
• Java Database Connectivity (JDBC)
• Java JDBC Tutorial
• JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with the
database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to connect with the
database. There are four types of JDBC drivers:
JDBC-ODBC Bridge Driver,
Native Driver,
Network Protocol Driver, and
Thin Driver
• We can use JDBC API to access tabular data stored in any relational database. By the help of JDBC API, we
can save, update, delete and fetch data from the database. It is like Open Database Connectivity (ODBC)
provided by Microsoft.
• The current version of JDBC is 4.3. It is the stable release since 21st September, 2017. It is based on the X/Open SQL Call Level
Interface. The java.sql package contains classes and interfaces for JDBC API. A list of popular interfaces of JDBC API are given
below:
• Driver interface
• Connection interface
• Statement interface
• PreparedStatement interface
• CallableStatement interface
• ResultSet interface
• ResultSetMetaData interface
• DatabaseMetaData interface
• RowSet interface
• A list of popular classes of JDBC API are given below:
• DriverManager class
• Blob class
• Clob class
• Types class
• Why Should We Use JDBC
• Before JDBC, ODBC API was the database API to connect and execute the query with the database. But,
ODBC API uses ODBC driver which is written in C language (i.e. platform dependent and unsecured). That is
why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).
• We can use JDBC API to handle database using Java program and can perform the following activities:
1. Connect to the database
2. Execute queries and update statements to the database
3. Retrieve the result received from the database.
• What is API
• API (Application programming interface) is a document that contains a description of all the features of a
product or software. It represents classes and interfaces that software programs can follow to communicate
with each other. An API can be created for applications, libraries, operating systems, etc.
• JDBC Driver
• JDBC Driver is a software component that enables java
application to interact with the database. There are 4 types of
JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
1) JDBC-ODBC bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to
the database. The JDBC-ODBC bridge driver converts JDBC
method calls into the ODBC function calls. This is now
discouraged because of thin driver.
• In Java 8, the JDBC-ODBC Bridge has been removed.
11. b.setBounds(80,150,60,50);
12. add(b);add(tf);
13. b.addActionListener(this);
14. setLayout(null); }
• The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu,
JColorChooser etc.
• Difference between AWT and Swing
• There are many differences between java awt and swing that are given below.
Method Description
public void setLayout(LayoutManager m) sets the layout manager for the component.
public void setVisible(boolean b) sets the visibility of the component. It is by default false.