从学代码到跑路
最近给组里的小伙伴做了一次培训,分享了一些编程的基础和自我学习编程的方法。感觉对大家可能有用,里面很有很面试经常问到的基础相关的东西,想分享给大伙。
计算机基础篇
首先看三段代码,分别为JS代码、C#代码和Java代码。有:局部变量、全局变量,值类型和引用类型,静态和常量、线程(JS没有)。
// 定义了一个Person类
function Person(id,name,age){
this.id = id;
this.name = name;
this.age = age;
}
function test(){
// 局部变量
let id = 0;
console.log(id);
}
// 全局变量
var person = new Person(100,'梁先生',25);
test();
// 定义了一个Person类
public class Person {
// 常量
public const string CodeTest = "梁先生";
// 静态变量
public static int Code = 0;
// 私有变量
private int testId;
public void add(){
// 局部变量
int temp = 0;
testId++;
}
}
// 线程类
public class Count implements Runnable{
Person person;
public Count(Person person){
this.person = person;
}
@Override
public void run(){
person.add();
}
}
var person = new Person(100,'梁先生',25);
// 多线程的问题
int threadCount = 100;
for(int i = 0; i<threadCount;i++){
ThreadMsDeal = new Thread(new Count(person));
ThreadMsDeal.Start();
}
// 定义了一个Person类
public class Person{
// 常量
public const string CodeTest = "梁先生";
// 静态变量
public static int Code = 0;
// 定义了一个私有变量
private testId = 0;
public Person(id,name,age){
this.id = id;
this.name = name;
this.age = age;
}
public int id {get;set;}
public string name{get;set;}
public int age{get;set;}
public void test(){
// 局部变量
int temp = 0;
testId++;
}
}
var person = new Person(100,'梁先生',25);
// 多线程的问题
int threadCount = 100;
for(int i = 0; i<threadCount;i++){
ThreadMsDeal = new Thread(new ThreadStart(test));
ThreadMsDeal.Start();
}
上面代码中的数据类型都能在下方的图找到对应。


JVM和CLR和很类似,使用JVM来说明

- new 的引用类型的值存在堆中。
- 普通值类型的如int啥的放在栈中。
- const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值。
- static限定,意味着“不管类的对象被创建多少次,都只有一个实例” 。
- 本地方法栈:虚拟机使用到的Native 方法服务。
- 常量和静态变量都是放在方法区里面的,这个区域也对所有线程共享。
js的V8虚拟机和上面两种有点像,但是又很不相同:JavaScript中所有的数据都是存放在堆内存中,为了基本数据类型和引用数据类型的理解区分开来。


值类型和引用类型


Q:为什么阿里巴巴Java开发手册不建议在for循环中使用“+”进行字符串拼接?

A:String是一个对象,且对象一旦被创建就是固定不变的了,对String对象的任何改变都不影响到原对象,所有一直在创建和销毁对象,改变指针的指向,浪费内存和性能。我们可以使用StringBuilder和StringBuffer,他们是可变的。
GC垃圾回收

- 如果不进行垃圾回收,内存迟早都会被消耗空
- 内存溢出:就是你要求分配的内存超出了系统能给你的,系统不能满足需求,于是产生溢出
- 内存泄漏:是指你向系统申请分配内存进行使用(new),可是使用完了以后却不回收
垃圾回收的几种方式




GC调优:很多次面试的时候有问道,gc垃圾回收的调优,你又了解嘛?
- 不要显式调用GC的回收函数,比如:Java中的 System.gc()、C#中的析构(2次回收)
- 尽量减少临时对象的使用(不使用的变量就给删掉)
- 对象不用时最好显式置为Null
- 不用大量的String来字符串拼接
- 能用基本类型如Int,Long,就不用Integer,Long对象
- 尽量少用静态对象变量,因为静态变量只有在程序结束时才释放
- 分散对象创建或删除的时间,比如避免:new 类[10000]
线程不安全



线程不安全:就是不提供数据访问保护,有可能出现多个线程先后更改数据造成所得到的数据是脏数据。
Q: i++是线程安全吗?
A: 是不安全的

每个线程都有自己的工作内存,每个线程需要对共享变量操作时必须先把共享变量从主内存 load 到自己的工作内存,等完成对共享变量的操作时再 save 到主内存。比如上面的i,是先拷贝到线程中的,然后做完++以后,不是很及时的刷新到主存。
问题就出在这了,如果一个线程运算完后还没刷到主内存,此时这个共享变量的值被另外一个线程从主内存读取到了,这个时候读取的数据就是脏数据了,它会覆盖其他线程计算完的值。
线程安全必要条件

原子性:跟数据库事务的原子性概念差不多,即一个操作(有可能包含有多个子操作)
要么全部执行(生效),要么全部都不执行(都不生效)。
可见性:当多个线程并发访问共享变量时,一个线程对共享变量的修改,
其它线程能够立即看到。可见性问题是好多人忽略或者理解错误的一点。
顺序性:是程序执行的顺序按照代码的先后顺序执行。

原子性解决:保证操作原子性的工具是锁和同步方法(或者同步代码块)。使用锁,可以保证同一时间只有一个线程能拿到锁,也就保证了同一时间只有一个线程能执行申请锁和释放锁之间的代码。 Java中的synchronized、c#中的lock
可见性解决:volatile关键字来保证可见性。当使用volatile修饰某个变量时,它会保证对该变量的修改会立即被更新到内存中,因此其它线程需要读取该值时必须从主内存中读取,从而得到最新的值。
高并发,目前比较流行的有两种,一种为多线程的方式(java中后台就是这样的),一种是事件驱动,异步IO(node中是这样的)。
高并发-多线程


线程的在操作系统的调度中,某个时刻,只能将资源让给一个线程。而线程的上下文的切换,会有很大的消耗。
高并发-异步IO


只有一个线程,没有上下文的切换消耗,但是只有一个线程干活。
选择场景如果 IO时间多,那异步IO效率高,选择场景如果 计算时间多,那同步IO效率高。Nginx 和 Node就是基于这种模式
常用的数据结构
- 数组(数组不要定义太长,因为需要连续的空间。不够的时候会引起GC)
- 栈(递归的时候利用的是栈)
- 队列(日志的读写分离)
- 链表
- 树 (菜单递归建树、数据库索引)
- 哈希表(查询数据快,只需要一次哦)
时间复杂度

这个方法需要 (n + 1 + n + 1) = 2n + 2 次运算。我们一般会把系数和尾巴去掉,因为在n趋近无穷大的时候,系数和尾巴都不咋起作用,差不了多少。故:时间复杂度为:O(n)
void Func(int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
printf("Hello, World!\n");
}
}
}
时间复杂度为:O(n × n × 1),即 O(n^2)。:
数据结构的时间复杂度

时间复杂度的图像,根据自己的编码情况选择对应的数据结构

大家可以对比一下,在x轴趋于无穷大时,x为数据量,而O(1)和log(n)的y值还只是1或者很小很小,y为查询的操作的次数。所以大家可以选择一定的数据结构来优化自己的代码
面向对象篇
在编码的时候去理解和运用一下三个特征。因为培训例子是举的工作中的例子,不咋好拿代码出来讲解。在我们发现有相同的代码的时候,我们是可以抽象出来的。还有一个是:对接口编程,而不是对实现编程。

五大原则也是需要一定的编码去支撑,然后才能自己的理解

还有常用的设计模式,这个也是很重要的,需要掌握常用的,因为时间问题,不在这里讲解。
总结篇
这个是个人总结,和语言无关,和框架无关。个人还是觉得基础还是很重要的,如果你懂基础,在代码优化,和出问题的时候去排查,站的角度是不一样的。
可能涉及到的书或者知识点比较多,但是没有必要全看完整的东西 ,只需要掌握基础,常用的知识点即可。不常用的东西,或者都不会涉及到的东西就不用看。
很多如代码重构,优化,是可以一直贯穿在整个图中的。

下方是是个人的一些学习方法和经常看到一些知识点的出处。

希望能帮助到大家,谢谢!
https://github.com/liangwei0101 可以的话,给个星星哦!