Java multi-threading concepts, advantages and disadvantages, multithreaded status | music bytes

Hello everyone, happy to have a small music bytes slightly, finished last Java Network Programming inquiry | music bytes , this time we take a look at the thread associated with it.

Content focuses on Java threads are:

1, the concept of threads

Multi-threaded, it means more than one path of execution, turned out to be a path to the main path (main), now is the multiple paths. Is equivalent to the highway, it turned out to be one way, because the car more, to improve efficiency, make full use of this road, the middle of a fence, into multiple lanes.

Note: All of the cars share this routine.

The original implementation of the program is a path now added a number of multi-threaded execution paths there.

1), a path

Method calls between one path, such as:

public class OneRoute {
public static void main(String[] args) { System.out.print("只有"); int result =a(); System.out.print("-->"+result); } public static int a(){ System.out.print("main"); return b()*100; } public static int b(){ System.out.print("一条路径"); return 1; } }

2), two paths

Open up a new thread, the execution path for the two.

public class ThreadRoute {
public static void main(String[] args) { System.out.print("多条路径"); new Route().start(); System.out.print("main-->"); } } class Route extends Thread{ public void run() { a(); } public int a(){ System.out.print("thread"); return b()*100; } public int b(){ System.out.print("一条路径"); return 1; } }

2, procedures, processes and multithreading

Program is a collection, a set of code instructions; and the process is a dynamic concept, when the program is executed, the system is dispensed into

Cheng; Multithreading is in the same process, full use of resources, multiple execution paths, shared resources (cpu data code).

note:

Many Multithreading is simulated, the real multi-threading refers to multiple cpu, namely multi-core, such as servers. If it is multi-threaded simulation out, in the case of a cpu that is, at the same time, cpu can only execute code, because soon switched, so there is executed simultaneously illusion.

 

3, multi-threaded advantages and disadvantages

1), the advantage of

Better resource utilization; programming easier in some cases; the program more responsive

2), shortcomings

a) the design more complex although there are some multi-threaded applications easier than single-threaded applications, but other general

They are more complex. When multiple threads access shared data, this part of the code needs special attention. To the interaction between threads

To the very complex. Incorrect thread synchronization error generated very difficult to find, reproduce and to repair.

b) the overhead of context switching when the CPU executes a handover from the thread to the execution of another thread when it requires

The current thread previously stored local data, and other program pointer, then load another thread local data, program counter

And so on, and finally started. This switching is called "context switching" ( "context switch"). The CPU in a

A context executing a thread, and then switch to another thread performs a further context. Context switching

并不廉价。如果没有必要,应该减少上下文切换的发生。

 

4、多线程地位

线程在 java 中的地位非常重要,但是比较难以理解,庆幸的是在实际工作中,直接编

写线程的代码不多;线程在手写服务器用的很多,当然现在的服务器都是其他公司写好的,

直接拿来用即可,如果真的想把多线程学好,需要掌握很多知识,如操作系统原理、并发机

制等。 对于目前的大家来说,线程掌握以下即可:

  • 创建线程的方式
  • sleep
  • 停止线程
  • Thread.currentThread
  • 简单的 synchronized

关于Java多线程的概念和优缺点就介绍到这,下一篇将会讲讲如何创建多线程。谢谢大家,请关注乐字节!

Guess you like

Origin www.cnblogs.com/lotbyte/p/11323474.html