What is Java thread


A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

Difference between “implements Runnable” and “extends Thread” in java

In java language, as we all know that there are two ways to create threads. One using Runnable interface and another by extending Thread class.

There has been a good amount of debate on which is better way. Well, I also tried to find out and below is my learning:

1) Implementing Runnable is the preferred way to do it. Here, you’re not really specializing or modifying the thread’s behavior. You’re just giving the thread something to run. That means composition is the better way to go.

2) Java only supports single inheritance, so you can only extend one class.

3) Instantiating an interface gives a cleaner separation between your code and the implementation of threads.

4) Implementing Runnable makes your class more flexible. If you extend thread then the action you’re doing is always going to be in a thread. However, if you extend Runnable it doesn’t have to be. You can run it in a thread, or pass it to some kind of executor service, or just pass it around as a task within a single threaded application.

5) By extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same runnable instance.

6) If you are working on JDK 4 or lesser, then there is bug :

Crop Image Using Java

 import java.awt.image.BufferedImage;  
 import java.io.File;  
 import java.io.IOException;  
 import javax.imageio.ImageIO;  
 public class test {  
      public static void main(String[] args) throws IOException {  
           test t = new test();  
           t.getCropImage();  
      }  
      public void getCropImage() throws IOException {  
           int widthOfNewImage = 100;  
           int heightOfNewImage = 100;  
           int offsetFromLeft = 10;  
           int offsetFromTop = 100;  
           String path = "input.gif";  
           String newPath = "newImage.png";  
           String imageFormat = "png";  
           BufferedImage image = ImageIO.read(new File(path));  
           int height = image.getHeight();  
           int width = image.getWidth();  
           System.out.println("Height : " + height + "\nWidth : " + width);  
           BufferedImage out = image.getSubimage(offsetFromLeft, offsetFromTop,  
                     widthOfNewImage, heightOfNewImage);  
           ImageIO.write(out, imageFormat, new File(newPath));  
      }  
 }  

Import CSS and JQuery files in JSP

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
 <link type="text/css" rel="stylesheet"href="${pageContext.request.contextPath}/css/bootstrap.min.css" />  
 <script type="text/javascript"src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>  
 <title>Concept Extractor</title>  
 <script type="text/javascript">  
  $(document).ready(function() {  
  alert("asdf");  
  });  
 </script>  

How to change eclipse background Theme

Installation

If you are on Eclipse 3.6 (Helios), the easiest way to install the plugin is from the Eclipse Marketplace. Go to Help→Eclipse Marketplace..., then search for Eclipse Color Theme and install it.
If you are on Eclipse 3.5 (Galileo), go to Help→Install New Software..., press Add Site and enterEclipse Color Theme as the name and http://eclipse-color-theme.github.com/update as the URL. Then select the new entry from the select box labeled Work with, mark Eclipse Color Theme for installation and proceed.
Please note: If you are using a version of the plugin lower than 0.6, please uninstall and reinstall it following the instructions above. Update site and plugin ID have changed.

Usage

After the installation, go to Window→Preferences→General→Appereance→Color Theme to change the color theme.
If you have a feature request, create an issue or a pull request on GitHub.

Is it possible to get HTML of an iframe - CAN'T

You are trying to access content of iframe which points to webpage from another domain. You cannot access content of iframe if the src of that iframe is not pointing to the domain on which your current parent page is. This is called cross domain policy

You will have to use server side language that will grab the html of given url and return it to your index page to display in any div or whatever.

Let me give you an example to explain why javascript cannot have cross domain access.
  • Suppose i have FB like box on my website inside an iframe
  • now whenever any user comes on my website i will trigger a click to the like box which is inside the iframe of like box.
  • This way my FB page will have lakhs of likes.
  • But because of cross domain policy this cannot be done.

Deployment of Apache Tomcat 7 in Amazon server


 Before Installation Check your java Jdk installed or not.  
 Step 1:  
 Download the Apache tomcat 7 from the bellow link,  
 Step2:  
 md5sum apache-tomcat-[#].tar.gz  
 Next, extract the package:  
 tar xvzf apache-tomcat-[#].tar.gz  
 And move the extracted folder into a dedicated directory:  
 sudo mv apache-tomcat-[#] /usr/local/apache  
 Step3: Set the path   
 vi ~/.bashrc  
 export CATALINA_HOME=/usr/local/apache  
 Log out and log back into bash to have your changes take effect.  
 Step4: Start tomcat  
 sudo /usr/local/apache/bin/startup.sh  
 Step 5 : Check  
 Go to web browser and type ,  
 http://localhost:8080  

How to remove sudo password :



For the administrative password using sudo and sparing any lectures on why one would not want this...
Edit the sudoers file:  sudo visudo

Find this line:%sudo ALL=(ALL) ALL

Change the line:%sudo ALL=(ALL) NOPASSWD: ALL

Save and Exit. Voila! (Dont' shoot yourself in the foot, now. ;) By the way, you can become root and just type the password once.sudo su -  Now you ARE the root user, seeing no more password prompts. When you see guides referring to commands such as sudo some_command, just remove the "sudo" portion. In this way, you can choose to leave the security intact yet bypass it as you see fit.

If you are writing about your user account:

Open System Settings. Click on the User Accounts tile. Click the Unlock button and enter your password. Set the auto-login slider to the "on" position by dragging it to the right. Then click "Lock" to apply your changes.

Install Oracle java using terminal

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

Deployment of Glassfish In amazon server

GlassFish is an open-source application server project started by Sun Microsystems for the Java EE platform and now sponsored by Oracle Corporation. The supported version is called Oracle GlassFish Server. GlassFish is free software, dual-licensed under two free software licences: the Common Development and Distribution License (CDDL) and the GNU General Public License (GPL) with the classpath exception.

GlassFish is the reference implementation of Java EE and as such supports Enterprise JavaBeans, JPA, JavaServer Faces, JMS, RMI, JavaServer Pages, servlets, etc. This allows developers to create enterprise applications that are portable and scalable, and that integrate with legacy technologies. Optional components can also be installed for additional services.




 Step 1:   
 Connect the Amazon server through terminal using DNS and the KEY.  
 To connect with amazon server:  
 ssh -i <key_path> <username>@<ip_adress_server>  
 First of all we need to check the java dependencies.  
 sudo apt-get update  
 sudo apt-get remove openjdk-6-jre-headless  
 sudo apt-get install sun-java6-jdk sun-java6-jre sun-java6-javadb  
 sudo apt-get autoremove  
 Step 2:  
 go to terminal in Ubuntu  
 go to your home directory in terminal   
 enter wget http://download.java.net/glassfish/4.0/release/glassfish-4.0.zip to download the glassfish in to your server.  
 Step 3:  
 Configure and change default port for Glassfish if tomcat already use port 8080.Because usually it use the port 4848.If you need as default please avoid thid Step 3.  
 vim glassfishv3/glassfish/domains/domain1/config/domain.xml  
 <network-listeners>  
 <network-listener port="8081" protocol="http-listener-1" transport="tcp" name="http-listener-1" thread-pool="http-thread-pool" />  
 <network-listener port="8181" protocol="http-listener-2" transport="tcp" name="http-listener-2" thread-pool="http-thread-pool" />  
 <network-listener port="4848" protocol="admin-listener" transport="tcp" name="admin-listener" thread-pool="http-thread-pool" />  
 </network-listeners>  
 Step 4:  
 Running by Glassfish  
 ./glassfishv3/glassfish/bin/asadmin start-domain --verbose  
 Step5:  
 Go to your-domain:8081 to see web page and your-domain:4848 for administrator pages  
 Glassfish Controls :  
 Create domain  
 Start and Stop Domain  
 Delete Domain  
 Enable secure Admin  

 Error may occur:  
 Solution:  
 http://charleech.wordpress.com/2012/03/23/glassfish-version-3-1-2-secure-admin-must-be-enabled-to-access-the-das-remotely/  
 http://coders-kitchen.com/tag/secure-admin-must-be-enabled-to-access-the-das-remotely/