Wednesday, 22 October 2014

多线程

进程与线程
多进程:在操作系统中能(同时)运行多个任务(程序)
多线程(multiple thread):在同一应用中有多个顺序流(同时)执行

线程lifecycle
线程类

//使用线程的方法一:继承Thread类
class FirstThread extends Thread(){
    //编写Run方法
    public void run(){
        for(int i = 0; i < 100; i++){
            System.out.println("");
        } 
    }
}

class Test{
    public static void main(String args[]){
        //生成线程类对象
        FirstThread ft = new FirstThread();
        //启动线程 ,使用start()方法,继承于Thread class
        //ft.run(); 单线程,不能这样写
        ft.start();//多线程
    }
}
Test class里有三个线程,将两个,一个是ft对象代表的线程,一个是Main()线程,(还有一个垃圾回收线程),CPU资源的分配是随机的。

//使用线程的方法二:接口Runnable
class runnableImp1 implements Runnable{
    public void run(){
        for(int i = 0; i < 100; i++){
        System.out.println("...");
        }
    }
}

class Test{
    public static void main(String args[]){
    //生成一个Runnable接口实现类的对象
    RunnableImpl ri = new RunnableImpl();
    //生成一个Thread对象,并将Runnable接口实现类的对象作为参数
    //传递给该Thread对象
    Thread t = new Thread();
    //通知Thread对象,执行start方法
    t.start();
    }
}

Java只支持单继承,推荐使用Runnable接口类

线程的简单控制方法
 - 中断线程
    Thread.sleep()
    Thread.yield()
 - 设置线程的优先级(1 - 10)
    getPriority()
    setPriority() by default, 优先级为5
优先级越大,执行的概率越大。

线程访问安全性
class MyThread implements Runnable{
    int i = 100;
    public void run(){
        while(true){
            //同步代码块
            synchroized(this){
                System.out.println(Thread.currentThread().getName() + i);
                i--;
                Thread.yield();
                if(i < 0){
                    break;
                }
            }
        }
    }
}

class Test{
    public static void main(String args[]){
        MyThread myThread = new MyThread();
        //生成两个Thread对象,共用同一个线程体
        Thread t1 = new Thread(myThread);
        Thread t2 = new Thread(myThread);
        //每一个线程都有名字,可以通过Thread对象setName()设置线程名字,也可以使用getName()获取线程的名子。
        t1.setName("线程a");
        t2.setName("线程b");
 
        t1.start();
        t2.start();
    }
}

多线程访问同一份资源,会出现多线程错误。解决方法:使用同步锁synchronized(this)

深入synchronized关键字
同步代码块(锁住对象)与同步方法作用类似,区别是同步代码块直接指定对象,同步方法也是指this。

Monday, 20 October 2014

DCI - Distributed Computing Infrastructure

Characteristics of application
Applications are:
 - Workflow solutions
 - Enterprise solutions
 - Customer Relationship solution

Customers => Sever => Database

Traditional Client / Server Environment(linked by a network using protocol such as Microsoft Network)
Strength(Application run on Local Area Environment)
 - high network speed as compared to over the Internet
 - easy of ensuring security
 - consistent UI using Windows
 - well controlled/consistent client platform
 - good control of starting and ending of application (session management)
Weakness
  - Specific client software required on each Individual terminal(difficult in implementation and maintenance)

Web-based Client / Server Environment(linked by a network using Internet protocol - TCP/IP)
Strength
 - application running on standard Intranet/Internet protocol(TCP/IP)
 - easy implementation and maintenance
 - consistent UI on different platforms(browser UI)
Weakness
 - lower network speed as compared to LAN
 - difficult to ensure security(over public switching telephone network)

Internet Protocol
TCP: Transmission Control Protocol
IP: Internet Protocol
HTTP: HyperText Transport Protocol
HTTPS: HyperText Transport Protocol Secure
Cookie Format(Write onto client machine):
Set-Cookie: NAME=VALUE; expired=DATE; path=PATH; domain=DOMAIN_NAME; secure
 - expires syntax must be "Weekday, dd-mmm-yyyy hh:mm:ss GMT"
 - domain must have at least 2 dots(3 dots for domains outside USA)
 - path refers to a URL path within the Web server
 - secure, if present, limits transmission to secure server

Internet 
Plug-ins: are programs modules loaded by the browser to handle specific file-formats.
Internet application and Intranet application

Empowering the Client
Scripting (no compilation needed)
          JavaScript (Netscape) - supported by almost every browsers, such as IE, Netscape, etc
          VBScript (Microsystem)
Components (compilation needed) - IE
          Java Applet (Sun Microsystem)
          ActiveX Control (Microsoft)


Wednesday, 15 October 2014

Using LINQ or Lambda Expressions to Entities - Entity Framework Query Options

LINQ to Entities
a. Create new entityframework(EF) called DefestyEntity;
b. Tables generated by EF are Customers, Orders, and so on;
c. Queries must be well defined at compile time;

Codes:
DafestyEntity ctx = new DafestyEntity();

var query = from x in ctx.Orders
                    where x.Customer.CustomerID == "Tom"
                    select x;
foreach (var x in query)
    Console.WriteLine("{0} {1}", x.OrderID, x.OrderDate);

LINQ Key Words
- from
  Specifies a data source and a range variable (similar to an iteration variable).
- where
  Filters source elements based on one or more Boolean expressions separated by logical AND and OR operators(&& or ||)
- select
  Specifies the type and shape that the elements in the returned sequence will have when the query is executed.
- group
  Groups query results according to a specified key value.
- into
  Provides an identifier that can serve as a reference to the results of a join, group or select clause.
- order by
  Sorts query results in ascending or descending order based on the default comparer for the element type.
- join
  Joins two data sources based on an equality comparison between two specified matching criteria.


Lambda Expressions
a. A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

Codes:
ctx.Orders.Where(x => x.CustomerID =="Tom");