donaricano-btn

Creating thread

- There are two way to create a thread

1) By extending Thread class

2) By implementing Runnable interface


1. Runnable interface

- Runnable interface have only one method named run()

1) run()

- It is used to perform action for a thread


2. Starting a thread

1) start()

- It is used to start a newly created thread

- It performs following task

a. A new thread starts(with new callstack)

b. The thread moves from New State to the Runnable state

c. When the thread gets a chance to execute, its target run() mehtod will run


3. By extending Thread class


4. By implementing Runnable interface

 

- If you are not extending the Thread class, your class object would not be treated as a thread object

- you need to explicitely create Thread class object

'Back-End > Java_1' 카테고리의 다른 글

[Java] Sleeping a thread  (0) 2016.10.07
[Java] Thread Scheduler  (0) 2016.10.07
[Java] Life cycle of a thread  (0) 2016.10.05
[Java] Multithreading in java  (0) 2016.10.05
[Java] Call by value  (0) 2016.09.13
블로그 이미지

리딩리드

,
donaricano-btn

Life cycle of a thread

- According to sun, there is only 4 states, new, runnable, non-runnable and terminated

- but this post I referred consist of 5 states to better understand the threads

1. New

- The thread is in new state 

- If you create an instance of Thread class 

2. Runnable

- The thread is in runnable state after invocation of start()

- the thread scheduler has not selected it to be the running thread

- sleep done, I/O complete, lock available, resume, notify

3. Running

- The thread is in running state

- The thread scheduler has selected it

4. Non-Runnable(Blocked)

- This is the state when the thread is sill alive

- it is currently not eligible to run

- sleep, block on I/O, wait for lock, suspend, wait

5. Terminated

- Dead state when its run() method exits


'Back-End > Java_1' 카테고리의 다른 글

[Java] Thread Scheduler  (0) 2016.10.07
[Java] Creating thread  (0) 2016.10.05
[Java] Multithreading in java  (0) 2016.10.05
[Java] Call by value  (0) 2016.09.13
[Java] Encapsulation  (0) 2016.09.12
블로그 이미지

리딩리드

,
donaricano-btn

Multithreading in java


1. Advantage of Java Multithreading

- It doesn't block the user because threads are independent and you can perform multiple operations at same time

- You can perform many operations together so it saves time

- Threads are independent so it doesn't affect other threads if exception occur in a single thread


2. MultiTasking

- Multitasking is a process of executing multiple tasks simultaneously

- We use it to utilize the CPU

- Multitasking can be achieved by two ways

1) Process-based Multitasking(Multiprocessing)

2) Thread-based Multitasking(Multithreading)


1) Process-based Multitasking(Multiprocessing)

- Each process have its own address in memory i.e. each process allocates separate memory area

- Process is heavyweight

- Cost of communication between the process is high

- Switching from one process to another require some time for saving and loading registers, memory maps...

2) Thread-based Multitasking(Multithreading)

- Threads share the same address space

- Thread is lightweight

- Cost of communication between the thread is low


3. Thread

- A thread is a lightweight sub process

- smallest unit of processing

- Threads are independent, if there occurs exception in one thread, it doesn't affect other threads

- It shares a common memory area

- Thread is executed inside the process

- There can be multiple processes inside the OS and One process can have multiple threads

- At a time one thread is executed only


'Back-End > Java_1' 카테고리의 다른 글

[Java] Creating thread  (0) 2016.10.05
[Java] Life cycle of a thread  (0) 2016.10.05
[Java] Call by value  (0) 2016.09.13
[Java] Encapsulation  (0) 2016.09.12
[Java] Package  (0) 2016.09.12
블로그 이미지

리딩리드

,
donaricano-btn

 Interceptor(3) - 인터셉터 등록과 설정

- 작성한 인터셉터는 struts.xml에 등록해야 한다



1. 인터셉더 등록

- name 속성으로 임의의 이름을 지정

- class 속성으로 인터셉터를 완전 수식 클래스 이름을 지정


2. 액션 적용

- interceptor-ref 태그로 등록한 fooInterceptor를 지정

- 인터셉터를 지정하면 지정한 인터셉터만 액션에 지정된다

1) defautStack

- 스트럿츠2는 기본적으로 다양한 이터셉터를 적용

- 동작을 바꾸고 싶지 않다면 기본 인터셉터(defaultStack)을 명시적으로 지정


3. 인터셉터 스택의 정의

- 임의의 인터셉터를 조합해서 인터셉터 스택 정의

 

1) 인터셉터 스택 정의

- fooStack으로 인터셉터 스택 정의

- 패키지안의 모든 액션에 적용할 떄는 기본 인터셉터 스택으로 하는게 효율적

2) fooStack을 기본 인터셉터 스택으로 설정

- 스트럿츠 패키지안의 모든 액션에 암묵적으로 fooStack이 적용


블로그 이미지

리딩리드

,