Threading in Java

Every thread in Java needs a Runnable. To make a runnable, you must first create a class which implements the Runnable interface. Runnable interface has a method called run() which gets called by the Thread. Your class will need to have this method. Put your code in this run() method. Your Runnable class should look similar to:

Now, you can create a thread in multiple ways but the most appropriate way is to pass the runnable object in its constructor.

Make sure that you say thread1.start() and not thread1.run(). thread1.run() simply calls the run method within the runnable while thread1.start() runs the run method in separate thread.

That’s it! Now you know the basics of threading in Java!

Check out the video tutorial here:

Using SCLC (Source Code Line Counter)

Well, it is really very simple. Here you go:

  1. Download the file. You can get it from here.
  2. Double click it. It should open as any other normal application.
    1. If it doesn’t open as expected or opens WinRAR/WinZIP/7-Zip or any other archiving software, follow here:
      1. Right click the file and point to Open With.
      2. Click on Select Default Program.
      3. Java should be here in the list of programs. If it isn’t then click browse and locate your java installation. It should be in C:Program FilesJavajre7binjava.exe
        1. If you dont have any folder called Java in Program files, then go to www.java.com and download and install it first.
        2. Once Java is installed, follow step 2.
      4. Click OK. Follow step 2.
  3. Click on ‘Add File’ button to add files to it. A dialog box will be presented.
  4. Locate your source code files. You can select more than one file by selecting one file and then holding the ‘Ctrl’ key and selecting other files. Alternatively, if you wish to select all files, you can press ‘Ctrl + A’ to select all files in the directory you are in.
    1. Make sure that you do not select any executable files. This may cause an error.
  5. Click OK/Open.
  6. You should see list of files in the list above ‘Add File’ button. If you wish to add more files, follow step 3.
  7. Once you are done, click Start. It will start counting lines in the files you have selected.
  8. You’re done! The table will display number of code lines in each file and below the table you’ll find total line count and line density.

I hope you like it. Feel free to shout out any suggestions/feedback you have regarding this small application. Have fun!

If statements in Linux Shell Script

If statements are important in any programming language. I had a bit of hard time while doing if statements in linux since they are different than those in a conventional programming language. First of all, an if statement to compare whether a value in a variable val1 is equal to 5 is like:
val1=5
if [ $val1 -eq 5 ]
then
     Code here to do some stuff
else
     If above condition is false, then code here gets executed
fi

Note that spaces are important when defining condition. $val1 refers to the variable and -eq simply implies equal to operator to compare if the $val1 is equal to 5.

Seems pretty easy? It is. Other conditional operators are:

Conditional Operator Linux Equivalent
Greater than -gt
Less than -lt
Equal to -eq
Not equal to -ne

Now, if I want to compare strings, for instance, if variable answer is equal to “yes” then the code will be as follows:
if [ "$val" = "yes" ]
then
     Code here to do some stuff
fi

Again, spaces are important here.
In here, ‘=’ can easily be replaced by conventional conditional operators like ‘>’ or ‘<‘ or ‘!=’ etc.

Variables in Linux Shell

Well, this has been a bit tricky for me so I decided to put it on my blog so that it can be helpful to you guys.

First of all, How to define a variable?


count=0

There! I’ve just defined a variable count with 0 as its initial value.
Now, How do you print value of a variable?

echo "$count"

or

echo $count

Next. How do you perform calculations on a variable?
Okay. So assume that you have two variables called val1 and val2 with values 3 and 4 respectively. Now, you want val3 to have the addition of val1 and val2. So,


val1=3
val2=4
val3=expr $val1 + $val2

Similarly, to do subtraction:

val4=expr $val1 - $val2

Note that spaces are very important here.

Now, if you wish to assign value returned by a command to a variable, then it works by doing:

wcount=wc -w myfile.txt

anything between and will be executed as linux command and the value returned by that command will be assigned back to the variable to left.

How to extract words from a string in C# (Split string)

This is incredibly easy in C#. First of all, you need to have a string from which you want to extract words. A word is a string separated by spaces. To do that, just say:

string s = "Welcome to my blog!";

Now, when I use the Split command, it will return an array of individual strings. So, the implementation will be:

string[] words = s.Split(" ",StringSplitOptions.RemoveEmptyEntries);

As a result, words array will contain “Welcome”, “to”, “my”, “blog!”.