Unit 4
Unit 4
Every Java program has at least one thread (the main thread).
Advantages of Multithreading:
Timed Waiting: Student waiting for a defined time (scheduled break between
lectures).
thread.start();
Thread.sleep(5000);
obj.wait(1000);
Thread t1 = new Thread(()
-> {
// task
});
synchronized(object) { BLOCKED. } synchronized(obj){ t1.start();
obj.wait();} t1.join();
Creating Threads in Java:Extending the Thread
Class
class MyThread extends Thread {
public void run() {
System.out.println("Thread running by extending Thread class.");
}
}
Method Description
Causes the current thread to wait indefinitely until another
wait()
thread calls notify() or notifyAll()
notify() Wakes up one waiting thread on the same object’s monitor
notifyAll() Wakes up all waiting threads on the object’s monitor
Simple Workflow
Communication:
•Waiter waits until the dish is ready (wait).
•Chef notifies waiter as soon as the dish is ready (notify).
JDBC Drivers Overview
Platform
Type Name Description Performance
Dependency
JDBC-ODBC
1 Uses ODBC drivers Dependent Lowest
Bridge
Native API Converts JDBC calls into
2 Dependent Moderate
Driver native API
Middleware
Network
3 communicates via Independent Good
Protocol
network
Direct Java-based
4 Thin Driver Independent Best
communication
Type 1: JDBC-ODBC Bridge Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:DataSourceName", "user", "password");
Type 2: Native API Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:oci:@databaseName", "user", "password");
Type 3: Network Protocol Driver (Middleware Driver)
•Platform independent.
Class.forName("com.ids.jdbc.IDSDriver");
Connection con = DriverManager.getConnection("jdbc:ids://host:port/dbname", "user", "password");
Type 4: Thin Driver (Pure Java Driver)
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password");