Friday, May 28, 2010

Examples of Basic SQL Queries

Example 1: INSERT INTO PhoneNumbers (name, number) VALUES(‘john’, 2034329343);

Given tables as follows: Company (companyName, id); EmployeesHired(id, numHired, fiscalQuarter)

Example 2: SELECT companyName FROM Company, EmployeesHired WHERE Company.id = EmployeesHired.id AND fiscalQuarter = 4;

Example 3: SELECT companyName FROM Company WHERE id NOT IN (SELECT id from EmployeesHired WHERE numHired > 0);

Example 4: SELECT companyName, SUM(numHired) FROM Company, EmployeesHired WHERE Company.id = EmployeesHired.id GROUP BY companyName;

Tuesday, May 25, 2010

Multithreading

Classes that support multithreaded programming are defined in the System.Threading namespace in C#.

using System.Threading;

Creating a Thread

Instatiate an object of type Thread.Thread

public Thread( ThreadStart entrypoint)

Here, entrypoint is the name of the method that will be called to begin execution of the thread. ThreadStart is a delegate defined by:

public delegate void ThreadStart()

Once created, the new thread will not start running until you call its Start() method.

using System;
using System.Threading;
namespace CSharpThreadExample
{
    class Program
    {
        public static void run()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("In thread " + Thread.CurrentThread.Name + i);
                Thread.Sleep(1000);
            }  
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Main Thread Starting");
            Thread.CurrentThread.Name = "Main ";
            Thread t1 = new Thread(new ThreadStart(run));

            t1.Name = "Child";
            t1.Start();

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("In thread " + Thread.CurrentThread.Name + i);
                Thread.Sleep(1000);
            }

            Console.WriteLine("Main Thread Terminates");
            Console.Read();
        }
    }

Methods


• Suspend() -> Suspends the execution of a thread till Resume() is called on that.

• Resume() -> Resumes a suspended thread. Can throw exceptions for bad state of the thread.

• Sleep() -> A thread can suspend itself by calling Sleep(). Takes parameter in form of milliseconds. We can use a special timeout 0 to terminate the current time slice and give other thread a chance to use CPU time

• Join()-> Called on a thread makes other threads wait for it till it finishes its task.

Syncronizing Shared Objects

The lock(object) is used to synchronize shared data.

lock (objecttobelocked) { objecttobelocked.somemethod(); }

Here objecttobelocked is the object reference which is used by more than one thread to call the method on that object. The lock keyword requires us to specify a token (an object reference) that must be acquired by a thread to enter within the lock scope. When we are attempting to lock down an instance level method, we can simply pass the reference to that instance. (We can use this keyword to lock the current object) Once the thread enters into a lock scope, the lock token (object reference) is inaccessible by other threads until the lock is released or the lock scope has exited.

If we want to lock down the code in a static method, we need to provide the System.Type of the respective class.

public void PrintNumbers()
{
 lock (this)
 {
  for (int i = 0; i < 5; i++)
  {
    Thread.Sleep(100);
    Console.Write(i + ",");
  }
  Console.WriteLine();
 }
}


Source: The Code Project http://www.codeproject.com/KB/threads/thread_synchronization.aspx