1 of 56

Unit 5

Multithreading in Java

Concurrency and Synchronization, Java Thread Model: Thread priorities, Synchronization, Messaging, Main Thread, Creating thread: Implementing Thread using thread class and Runnable interface. Creating multiple threads using isAlive() and join()

Web Based Application in Java: Use of JavaScript for creating web based applications in java, Introduction to javascript frameworks- React, Vue, Angular

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

2 of 56

Application of Multithreading

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

3 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

4 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

5 of 56

  • A program with multiple threads running within a single instance could be considered as a multitasking system within an OS.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

6 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

7 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

8 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

9 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

10 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

11 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

12 of 56

class Create extends Thread

{

Create()

{

super("ABC");

start();

}// Constructor Closed

public void run()

{

try {

for(int i = 0;i<5;i++)

{ System.out.println("Child : " +i);

Create Thread by Extending Thread Class

Thread.sleep(500);

} //for close

}catch(InterruptedException e){ }

System.out.println("Child Exiting");

} //run() close

public static void main(String[] args)

{

Create c = new Create();

try {

for(int i = 0;i<5;i++)

{ System.out.println("Mainthread"+i);

Thread.sleep(1000);

} //for close

}catch(InterruptedException e) { }

System.out.println("Main Exiting");

} }

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

13 of 56

Child : 0

Mainthread0

Child : 1

Mainthread1

Child : 2

Child : 3

Mainthread2

Child : 4

Child Exiting

Mainthread3

Mainthread4

Main Exiting

...Program finished with exit code 0

Press ENTER to exit console.

Create Thread by Extending Thread Class..Output

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

14 of 56

class Create implements Runnable {

Thread t;

Create()

{

t = new Thread(this,”DemoThread”);

t.start();

}// Constructor Closed

public void run()

{

try {

for(int i = 0;i<5;i++)

{ System.out.println("Child : " +i);

Create Thread by Implementing Runnable

Thread.sleep(500);

} //for close

}catch(InterruptedException e){ }

System.out.println("Child Exiting");

} //run() close

public static void main(String[] args)

{

Create c = new Create();

try {

for(int i = 0;i<5;i++)

{ System.out.println("Mainthread"+i);

Thread.sleep(1000);

} //for close

}catch(InterruptedException e) { }

System.out.println("Main Exiting");

} }

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

15 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

16 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

17 of 56

Main Thread

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

18 of 56

The general form of currentThread method is :

static Thread currentThread();

Hence it can be invoked as follows :

Thread.currentThread();

For Ex :

Main Thread

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

19 of 56

class Demo

{

public static void main(String args[])

{

Thread t = Thread.currentThread( );

System.out.println(“The Thread is : “ +t);

}

}

O/p : The Thread is : Thread[main,5,main]

In the output Thread[main,5,main] : The order is :

[Name of thread , Priority of thread , & Group of thread]

Main Thread

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

20 of 56

Main Thread

class Demo

{

public static void main(String args[])

{

Thread t = Thread.currentThread( );

System.out.println(“The Thread is : “ +t);

t.setName(“First Thread”);

System.out.println(“The new name is : “ +t);

int p = t.getPriority( );

System.out.println(“The Thread priority is : “ +p);

t.setPriority(10);

System.out.println(“The new priority is : “ +t.getPriority( ));

}

}

OUTPUT

The Thread is : Thread[main,5,main]

The new name is : [First Thread,5,main]

The Thread priority is : 5

The new priority is : 10

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

21 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

22 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

23 of 56

Synchronization

class Table{

synchronized void printTable(int n){

//method not synchronized

for(int i=1;i<=5;i++){

System.out.println(n*i);

try{

Thread.sleep(400);

}catch(Exception e){System.out.println(e);}

} //for close

} }

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

24 of 56

Synchronization..>Contd

class MyThread1 extends Thread{

Table t;

MyThread1(Table t){

this.t=t;

}

public void run(){

t.printTable(5);

}

}

class MyThread2 extends Thread{

Table t;

MyThread2(Table t){

this.t=t;

}

public void run(){

t.printTable(100);

}

}

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

25 of 56

class TestSynchronization1{

public static void main(String args[]){

Table obj = new Table();//only one object

MyThread1 t1=new MyThread1(obj);

MyThread2 t2=new MyThread2(obj);

t1.start();

t2.start();

}

}

OUTPUT

5

10

15

20

25

100

200

300

400

500

Synchronization…Contd

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

26 of 56

wait()

join()

It is a method of java.lang.Object class.

It is a method of java.lang.Thread

wait() method can be called by a synchronized block or method.

It is used to stop the current thread.

wait() method is implemented for performing multi-thread-synchronization.

It can be called either with synchronized and without synchronized context.

Its syntax is -:

public final void wait() throws InterruptedException

Its syntax is -:

public final void join() throws InterruptedException

wait() method causes the thread to sleep until notify() and notifyAll() are called

It can be used to add sequence among more than one thread

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

27 of 56

Web Based Application in Java: Use of JavaScript for creating web based applications in java, Introduction to javascript frameworks- React, Vue, Angular

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

28 of 56

Web Application Development

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

29 of 56

Static VS Dynamic

  • HTML documents are usually static
  • The contents can only be changed manually
  • There are needs for dynamic documents
    • Search results
    • Database access
    • Context sensitive reply
  • Static
    • page appears exactly as it was encoded, nothing changes
  • Dynamic
    • page is compiled, or able to be changed

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

30 of 56

Scripts: Server-Side VS Client-Side

  • Server-side
    • the first type possible on the Web
    • action occurs at the server
  • Client-side
    • generally easier to implement
    • may be prepared and implemented offline
    • action occurs on the client side (browser)

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

31 of 56

JAVASCRIPT

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

32 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

33 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

34 of 56

LIBRARY Vs FRAMEWORK

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

35 of 56

JAVA SCRIPT

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

36 of 56

JAVASCRIPT FEATURES

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

37 of 56

NEED/ADVANTAGES OF JAVASCRIPT

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

38 of 56

Use to structure and give meaning to our web content, for example defining paragraphs, headings, and data tables, or embedding images and videos in the page.

To apply styling to our HTML content, for example setting background colors and fonts, and laying out our content in multiple columns.

To create dynamically updating content, control multimedia, animate images.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

39 of 56

<p>Player 1: Chris</p>

p {

font-family: 'helvetica neue', helvetica, sans-serif;

letter-spacing: 1px;

text-transform: uppercase;

text-align: center;

border: 2px solid rgba(0,0,200,0.6);

background: rgba(0,0,200,0.3);

color: rgba(0,0,200,0.6);

box-shadow: 1px 1px 2px rgba(0,0,200,0.4);

border-radius: 10px;

padding: 3px 10px;

display: inline-block;

cursor: pointer;

}

const para = document.querySelector('p');

para.addEventListener('click', updateName);

function updateName() {

const name = prompt('Enter a new name');

para.textContent = `Player 1: ${name}`;

}

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

40 of 56

JavaScript - Coding Ways/ Script Placement

Script in

<head>...</head> section.

Script in

<body>...</body> section.

Script in

<head>and <body> section.

Script in an external file and then include in <head>...</head> section.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

41 of 56

JavaScript - Coding Ways/ Script Placement

Script in

<head>...</head> section.

Script in

<body>...</body> section.

In HTML, JavaScript code is inserted between

<script> and </script> tags.

Script in

<head>and <body> section.

Script in an external file and then include in <head>...</head> section.

Note

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

42 of 56

JavaScript - Example 1

Script in

<head>...</head> section.

<html>

<head>

<script>

document.write("Hello World")

</script>

</head>

</html>

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

43 of 56

Example 2

Script in an external file and then include in <head>...</head> section.

<html>

<head>

<script type = "text/javascript" src = "filename.js" ></script>

</head>

<body>

<h2>Demo External JavaScript</h2>

<button type="button" onclick="sayHello()">Try it</button>

</body>

</html>

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

44 of 56

Example 2 : Output

Script in an external file and then include in <head>...</head> section.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

45 of 56

JavaScript - Vue

Open Source

Used to develop interactive web interfaces.

VueJS focusses on the view layer

It is easily integrated into big projects for front-end development without any issues.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

46 of 56

<html>

<head>

<title>VueJs Introduction</title>

<script type = "text/javascript" src = "js/vue.js"></script>

</head>

<body>

<div id = "intro" style = "text-align:center;">

<h1>{{ message }}</h1>

</div>

<script type = "text/javascript">

var vue_det = new Vue({

el: '#intro',

data: {

message: 'My first VueJS Task'

}

}); </script> </body> </html>

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

47 of 56

Example Vue : Output

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

48 of 56

JavaScript - AngularJs

Powerful JavaScript Framework.

Used in Single Page Application (SPA) projects

Open Source

It extends HTML DOM with additional attributes and makes it more responsive to user actions

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

49 of 56

JavaScript - AngularJs

It Uses Model View Controller Architecture

AngularJS framework can be divided into three major parts

  • Model - maintains the data
  • View : Display of data/Appearance of data
  • Controller : Interface between model and View

ng-app

ng-model

ng-bind

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

50 of 56

JavaScript - AngularJs

ng-app

ng-model

ng-bind

This directive defines and links an AngularJS application to HTML.

This directive binds the values of AngularJS application data to HTML input controls.

This directive binds the AngularJS application data to HTML tags.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

51 of 56

<html>

<head>

<title>AngularJS First Application</title>

</head>

<body>

<h1>Sample Application</h1>

<div ng-app = "">

<p>Enter your Name: <input type = "text" ng-model = "name"></p>

<p>Hello <span ng-bind = "name"></span>!</p>

</div>

<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">

</script>

</body>

</html>

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

52 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

53 of 56

  • Angular, developed by Google, was first released in 2010, making it the oldest of the lot. It is a TypeScript-based JavaScript framework.
  • React, developed by Facebook, was initially released in 2013. Facebook uses React extensively in their products (Facebook, Instagram, and WhatsApp).
  • Vue, also known as Vue.js, is the youngest member of the group. It was developed by ex-Google employee Evan You in 2014.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

54 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

55 of 56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

56 of 56

End of Unit 5

Thank You!!!

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering