16 December, 2012

Join Thread Example

Here in the below example, the threads t1 and t2 are accessing the common resource "Counter" without the "synchronized" keyword. Even then the output is coming sequential because of join.
It means that t2 will wait till t1 gets die.

public class Counter implements Runnable {
    private static int count = 0;
    @Override
    public void run() {

        for (int i = 0; i < 100; i++) {
            System.out.println(count++ + Thread.currentThread().getName());
        }
    }
}

public class JoinImpl {
    public static void main(String[] args) throws InterruptedException{
        Counter counter = new Counter();
        Thread t1 = new Thread(counter);
        Thread t2 = new Thread(counter);
        t1.start();
        t1.join();
        t2.start();
    }
}
---------------------------------
output
--------------------------------
0Thread-0
1Thread-0
2Thread-0
3Thread-0
4Thread-0
5Thread-0
6Thread-0
7Thread-0
8Thread-0
9Thread-0
10Thread-0
11Thread-0
12Thread-0
13Thread-0
14Thread-0
15Thread-0
16Thread-0
17Thread-0
18Thread-0
19Thread-0
20Thread-0
21Thread-0
22Thread-0
23Thread-0
24Thread-0
25Thread-0
26Thread-0
27Thread-0
28Thread-0
29Thread-0
30Thread-0
31Thread-0
32Thread-0
33Thread-0
34Thread-0
35Thread-0
36Thread-0
37Thread-0
38Thread-0
39Thread-0
40Thread-0
41Thread-0
42Thread-0
43Thread-0
44Thread-0
45Thread-0
46Thread-0
47Thread-0
48Thread-0
49Thread-0
50Thread-1
51Thread-1
52Thread-1
53Thread-1
54Thread-1
55Thread-1
56Thread-1
57Thread-1
58Thread-1
59Thread-1
60Thread-1
61Thread-1
62Thread-1
63Thread-1
64Thread-1
65Thread-1
66Thread-1
67Thread-1
68Thread-1
69Thread-1
70Thread-1
71Thread-1
72Thread-1
73Thread-1
74Thread-1
75Thread-1
76Thread-1
77Thread-1
78Thread-1
79Thread-1
80Thread-1
81Thread-1
82Thread-1
83Thread-1
84Thread-1
85Thread-1
86Thread-1
87Thread-1
88Thread-1
89Thread-1
90Thread-1
91Thread-1
92Thread-1
93Thread-1
94Thread-1
95Thread-1
96Thread-1
97Thread-1
98Thread-1
99Thread-1


Synchronized Thread Example


In the below example the threads(t1,t2) both are accessing the common shared resource simultaneously and increasing the shared variable "count" in a sequential order ( i.e. count getting increase from 0 to 49 by t1 and then count getting increase from 50 to 99 by t2).

public class Resource implements Runnable {
    private static int count=0;
    @Override
    public synchronized  void run() {
        try {
            Thread.sleep(1000);
            for (int i = 0; i < 100; i++) {
                System.out.println("count="+count+++"--->"+Thread.currentThread().getName());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class Process {
    public static void main(String[] args){
        Resource sharedResource = new Resource();
        Thread t1 = new Thread(sharedResource);
        Thread t2 = new Thread(sharedResource );
        t1.start();
        t2.start();
    }
}
----------------------------------
output
---------------------------------
count=0--->Thread-0
count=1--->Thread-0
count=2--->Thread-0
count=3--->Thread-0
count=4--->Thread-0
count=5--->Thread-0
count=6--->Thread-0
count=7--->Thread-0
count=8--->Thread-0
count=9--->Thread-0
count=10--->Thread-0
count=11--->Thread-0
count=12--->Thread-0
count=13--->Thread-0
count=14--->Thread-0
count=15--->Thread-0
count=16--->Thread-0
count=17--->Thread-0
count=18--->Thread-0
count=19--->Thread-0
count=20--->Thread-0
count=21--->Thread-0
count=22--->Thread-0
count=23--->Thread-0
count=24--->Thread-0
count=25--->Thread-0
count=26--->Thread-0
count=27--->Thread-0
count=28--->Thread-0
count=29--->Thread-0
count=30--->Thread-0
count=31--->Thread-0
count=32--->Thread-0
count=33--->Thread-0
count=34--->Thread-0
count=35--->Thread-0
count=36--->Thread-0
count=37--->Thread-0
count=38--->Thread-0
count=39--->Thread-0
count=40--->Thread-0
count=41--->Thread-0
count=42--->Thread-0
count=43--->Thread-0
count=44--->Thread-0
count=45--->Thread-0
count=46--->Thread-0
count=47--->Thread-0
count=48--->Thread-0
count=49--->Thread-0
count=50--->Thread-0
count=51--->Thread-0
count=52--->Thread-0
count=53--->Thread-0
count=54--->Thread-0
count=55--->Thread-0
count=56--->Thread-0
count=57--->Thread-0
count=58--->Thread-0
count=59--->Thread-0
count=60--->Thread-0
count=61--->Thread-0
count=62--->Thread-0
count=63--->Thread-0
count=64--->Thread-0
count=65--->Thread-0
count=66--->Thread-0
count=67--->Thread-0
count=68--->Thread-0
count=69--->Thread-0
count=70--->Thread-0
count=71--->Thread-0
count=72--->Thread-0
count=73--->Thread-0
count=74--->Thread-0
count=75--->Thread-0
count=76--->Thread-0
count=77--->Thread-0
count=78--->Thread-0
count=79--->Thread-0
count=80--->Thread-0
count=81--->Thread-0
count=82--->Thread-0
count=83--->Thread-0
count=84--->Thread-0
count=85--->Thread-0
count=86--->Thread-0
count=87--->Thread-0
count=88--->Thread-0
count=89--->Thread-0
count=90--->Thread-0
count=91--->Thread-0
count=92--->Thread-0
count=93--->Thread-0
count=94--->Thread-0
count=95--->Thread-0
count=96--->Thread-0
count=97--->Thread-0
count=98--->Thread-0
count=99--->Thread-0
count=100--->Thread-1
count=101--->Thread-1
count=102--->Thread-1
count=103--->Thread-1
count=104--->Thread-1
count=105--->Thread-1
count=106--->Thread-1
count=107--->Thread-1
count=108--->Thread-1
count=109--->Thread-1
count=110--->Thread-1
count=111--->Thread-1
count=112--->Thread-1
count=113--->Thread-1
count=114--->Thread-1
count=115--->Thread-1
count=116--->Thread-1
count=117--->Thread-1
count=118--->Thread-1
count=119--->Thread-1
count=120--->Thread-1
count=121--->Thread-1
count=122--->Thread-1
count=123--->Thread-1
count=124--->Thread-1
count=125--->Thread-1
count=126--->Thread-1
count=127--->Thread-1
count=128--->Thread-1
count=129--->Thread-1
count=130--->Thread-1
count=131--->Thread-1
count=132--->Thread-1
count=133--->Thread-1
count=134--->Thread-1
count=135--->Thread-1
count=136--->Thread-1
count=137--->Thread-1
count=138--->Thread-1
count=139--->Thread-1
count=140--->Thread-1
count=141--->Thread-1
count=142--->Thread-1
count=143--->Thread-1
count=144--->Thread-1
count=145--->Thread-1
count=146--->Thread-1
count=147--->Thread-1
count=148--->Thread-1
count=149--->Thread-1
count=150--->Thread-1
count=151--->Thread-1
count=152--->Thread-1
count=153--->Thread-1
count=154--->Thread-1
count=155--->Thread-1
count=156--->Thread-1
count=157--->Thread-1
count=158--->Thread-1
count=159--->Thread-1
count=160--->Thread-1
count=161--->Thread-1
count=162--->Thread-1
count=163--->Thread-1
count=164--->Thread-1
count=165--->Thread-1
count=166--->Thread-1
count=167--->Thread-1
count=168--->Thread-1
count=169--->Thread-1
count=170--->Thread-1
count=171--->Thread-1
count=172--->Thread-1
count=173--->Thread-1
count=174--->Thread-1
count=175--->Thread-1
count=176--->Thread-1
count=177--->Thread-1
count=178--->Thread-1
count=179--->Thread-1
count=180--->Thread-1
count=181--->Thread-1
count=182--->Thread-1
count=183--->Thread-1
count=184--->Thread-1
count=185--->Thread-1
count=186--->Thread-1
count=187--->Thread-1
count=188--->Thread-1
count=189--->Thread-1
count=190--->Thread-1
count=191--->Thread-1
count=192--->Thread-1
count=193--->Thread-1
count=194--->Thread-1
count=195--->Thread-1
count=196--->Thread-1
count=197--->Thread-1
count=198--->Thread-1
count=199--->Thread-1

Unsynchronized Thread Example

Un-Synchronized Threads means that the threads are running independently without gaining a "lock" on the shared resource.
In the below example the threads(t1,t2) both are accessing the common shared resource simultaneously and increasing the shared variable "count" in a random order rather than one by one( i.e. count getting increase from 0 to 49 by t1 and then count getting increase from 50 to 99 by t2). Here, count is getting increase to 14 by "Thread-0" and then count is getting increase to 15 by "Thread-1". 

public class Resource implements Runnable {
    private static int count=0;
    @Override
    public  void run() {
        try {
            Thread.sleep(1000);
            for (int i = 0; i < 50; i++) {
                System.out.println("count="+count+++"--->"+Thread.currentThread().getName());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class Process {
    public static void main(String[] args){
        Resource sharedResource = new Resource();
        Thread t1 = new Thread(sharedResource);
        Thread t2 = new Thread(sharedResource );
        t1.start();
        t2.start();
    }
}

----------------------------------------
output
----------------------------------------
count=0--->Thread-1
count=1--->Thread-0
count=2--->Thread-0
count=3--->Thread-0
count=4--->Thread-0
count=5--->Thread-0
count=6--->Thread-0
count=7--->Thread-0
count=8--->Thread-0
count=9--->Thread-0
count=10--->Thread-0
count=11--->Thread-0
count=13--->Thread-0
count=14--->Thread-0
count=12--->Thread-1
count=16--->Thread-1
count=15--->Thread-0
count=18--->Thread-0
count=19--->Thread-0
count=17--->Thread-1
count=21--->Thread-1
count=22--->Thread-1
count=20--->Thread-0
count=24--->Thread-0
count=23--->Thread-1
count=26--->Thread-1
count=27--->Thread-1
count=28--->Thread-1
count=29--->Thread-1
count=30--->Thread-1
count=31--->Thread-1
count=32--->Thread-1
count=33--->Thread-1
count=34--->Thread-1
count=35--->Thread-1
count=36--->Thread-1
count=37--->Thread-1
count=38--->Thread-1
count=39--->Thread-1
count=40--->Thread-1
count=41--->Thread-1
count=42--->Thread-1
count=43--->Thread-1
count=44--->Thread-1
count=45--->Thread-1
count=46--->Thread-1
count=47--->Thread-1
count=48--->Thread-1
count=49--->Thread-1
count=50--->Thread-1
count=51--->Thread-1
count=52--->Thread-1
count=53--->Thread-1
count=54--->Thread-1
count=55--->Thread-1
count=56--->Thread-1
count=57--->Thread-1
count=58--->Thread-1
count=59--->Thread-1
count=60--->Thread-1
count=61--->Thread-1
count=62--->Thread-1
count=63--->Thread-1
count=64--->Thread-1
count=65--->Thread-1
count=66--->Thread-1
count=67--->Thread-1
count=68--->Thread-1
count=25--->Thread-0
count=69--->Thread-0
count=70--->Thread-0
count=71--->Thread-0
count=72--->Thread-0
count=73--->Thread-0
count=74--->Thread-0
count=75--->Thread-0
count=76--->Thread-0
count=77--->Thread-0
count=78--->Thread-0
count=79--->Thread-0
count=80--->Thread-0
count=81--->Thread-0
count=82--->Thread-0
count=83--->Thread-0
count=84--->Thread-0
count=85--->Thread-0
count=86--->Thread-0
count=87--->Thread-0
count=88--->Thread-0
count=89--->Thread-0
count=90--->Thread-0
count=91--->Thread-0
count=92--->Thread-0
count=93--->Thread-0
count=94--->Thread-0
count=95--->Thread-0
count=96--->Thread-0
count=97--->Thread-0
count=98--->Thread-0
count=99--->Thread-0

29 July, 2012

example of ajax using struts2

----------------------- web.xml---------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Java</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>


----------------------- index.jsp---------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<script type="text/javascript">
    function get_random(){
        var ranNum= Math.floor(Math.random()*5);
        return ranNum;
    }
</script>
<head>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=example/showHomePage.action?rand=<%=(Math.random()) %>">
</head>
<body>
<p>Loading ...</p>
</body>
</html>



--------------------- struts.xml---------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <package name="example" namespace="/example" extends="struts-default">
        <action name="showHomePage" class="example.Login">
            <result>/example/LoginPage.jsp</result>
        </action>
        <action name="validateUsername" class="example.Login" method="validateUsername">
            <result>/example/resp.jsp</result>
            <result name="input">/example/resp.jsp</result>
        </action>
    </package>
</struts> 


------------------ LoginPage.jsp
------------------------------
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <script type="text/javascript">
        var xmlhttp;
        function getHTTPObjectForBrowser(){
            if (window.XMLHttpRequest){
                  xmlhttp=new XMLHttpRequest();
            }else{
                  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            return xmlhttp;
        }
        function setAjaxOutput(){
            if(xmlhttp.readyState == 4){
                var result = xmlhttp.responseText;
                if(result=='input'){
                    document.getElementById('username').value='';
                    document.getElementById('password').value='';
                    document.getElementById('username').focus();
                }
                if(result=='success'){
                    document.getElementById('result').value=result;
                }
            }
        }
        function validateUser(){
            xmlhttp= getHTTPObjectForBrowser();
            var username = document.getElementById('username').value;
            var password = document.getElementById('password').value;
           
            if (xmlhttp!= null) {
                var url = "validateUsername.action?username="+username+'&password='+password;
                xmlhttp.open("GET", url, true);
                xmlhttp.send(null);
                xmlhttp.onreadystatechange = setAjaxOutput;
            }
        }           
    </script>
</head>
<body>
    <s:form name="loginPage" id="LoginPage" action="loginSubmit">
        <s:textfield label="username" id="username"> </s:textfield>
        <s:textfield label="password" id="password" onblur="validateUser();"></s:textfield>
        <s:textfield label="result" id="result" name="result"></s:textfield>
    </s:form>
</body>
</html>
 


-------------------------- Login.java---------------------------
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
    private String resp;

    public String execute() {
        return SUCCESS;
    }

    public String validateUsername() {
        if (username.equals("admin") && password.equals("admin")) {
            resp = "success";
            return SUCCESS;
        } else {
            resp = "input";
            return INPUT;
        }
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setResp(String resp) {
        this.resp = resp;
    }

    public String getResp() {
        return resp;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }
}


-------------- resp.jsp-------------------------------------
${resp}




Screen shot:
----------------