知识点

选项 public protected default private 同一类中 ok ok ok ok 同一包中 ok ok ok 子类中 ok ok 不同包中 ok
创建新线程的执行方法:
一种方法是声明为Thread的子类,在子类中覆盖Run方法,将线程任务封装在run方法中
另一种方法是实现Runnable的接口类,该类然后实现run方法。将线程任务对象放于Thread的构造函数中。

线程的状态图

多线程容易产生安全问题:
当多个线程操作共享数据,和操作共享数据的代码有多条时,就容易产生安全问题。
synchronized(对象) { }
好处是解决了线程的安全问题,坏处是降低了线程的效率

同步的前提:同步中必须有多个线程并使用同一个锁。

(懒汉式)单例设计模式中的锁:
class Single { private static Single s; private Single{} public static Single getInstance() { synchronized(Single.class)//类名.class { if(s == null) { s = new Single(); } } return s; } }

写一个死锁的程序

class Test implements Runnable
{
    private boolean flag;
    Test(boolean flag)
    {
        this.flag = flag;
    }
    public void run()
    {
        if(flag)
        {
            synchronized(MyLock.locka)
            {
                System.out.println(Thread.currentThread().getName()+"if.... locka..");
                synchronized(MyLock.lockb)
                {
                    System.out.println(Thread.currentThread().getName()+"else.... locka..");

                }
            }
        }
        else
        {
            synchronized(MyLock.lockb)
            {
                System.out.println(Thread.currentThread().getName()+"if.... lockb..");
                synchronized(MyLock.locka)
                {
                    System.out.println(Thread.currentThread().getName()+"else.... lockb..");
                }
            }
        }
    }
}

class MyLock
{
    public static final MyLock locka = new MyLock();
    public static final MyLock lockb = new MyLock();
}

class DeadLock
{
    public static void main(String[] args) 
    {
        Test t = new Test(true);
        Test k = new Test(false);

        Thread t1 = new Thread(t);
        Thread t2 = new Thread(k);

        t1.start();
        t2.start();
    }
}

多生产多消费的多线程问题

class Resource
{
    private String name;
    private int count = 1;
    private boolean flag = false;
    public synchronized void set(String name)
    {
        while(flag)
            try{this.wait();}catch(InterruptedException e){}
        
        this.name = name + count;
        count++;
        System.out.println(Thread.currentThread().getName()+".生产者______"+this.name);
        flag = true;
        notifyAll();
    }
    public synchronized void out()
    {
        while(!flag)
            try{this.wait();}catch(InterruptedException e){}
        System.out.println(Thread.currentThread().getName()+".消费者..."+this.name);
    //  count--;
        flag = false;
        notifyAll();
    }
}

class Producer implements Runnable
{
    private Resource r;
    Producer(Resource r)
    {
        this.r = r;
    }
    public void run()
    {
        while(true)
        {
            r.set("烤鸭");
        }
    }
}

class Customer implements Runnable
{
    private Resource r;
    Customer(Resource r)
    {
        this.r = r;
    }
    public void run()
    {
        while(true)
        {
            r.out();
        }
    }
}

public class ThreadCom 
{
    public static void main(String[] args)
    {
        Resource r = new Resource();
        
        Producer a = new Producer(r);
        Customer b = new Customer(r);
        
        Thread t1 = new Thread(a);
        Thread t2 = new Thread(a);
        Thread t3 = new Thread(b);
        Thread t4 = new Thread(b);
        
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

String类:

String类中常用的方法:

  • public char charAt(int index)
    返回字符串中第index个字符;
  • public int length()
    返回字符串的长度;
  • public int indexOf(String str)
    返回字符串中第一次出现str的位置;
  • public int indexOf(String str,int fromIndex)
    返回字符串从fromIndex开始第一次出现str的位置;
  • public boolean equalsIgnoreCase(String another)
    比较字符串与another是否一样(忽略大小写);
  • public String replace(char oldchar,char newChar)
    在字符串中用newChar字符替换oldChar字符
  • public boolean startsWith(String prefix)
    判断字符串是否以prefix字符串开头;
  • public boolean endsWith(String suffix)
    判断一个字符串是否以suffix字符串结尾;
  • public String toUpperCase()
    返回一个字符串为该字符串的大写形式;
  • public String toLowerCase()
    返回一个字符串为该字符串的小写形式
  • public String substring(int beginIndex)
    返回该字符串从beginIndex开始到结尾的子字符串;
  • public String substring(int beginIndex,int endIndex)
    返回该字符串从beginIndex开始到endsIndex结尾的子字符串
  • public String trim()
    返回该字符串去掉开头和结尾空格后的字符串
  • public String[] split(String regex)
    将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组
String s2 = new String("2017/2/10");    
        String[] splitArr = new String[3];
        splitArr = s2.split("/");
        for(int i = 0;i < splitArr.length;i++)
        {
            System.out.println(splitArr[i]+",");
        }

Stringbuffer

1、append方法
public StringBuffer append(boolean b)
该方法的作用是追加内容到当前StringBuffer对象的末尾,类似于字符串的连接。调用该方法以后,StringBuffer对象的内容也发生改变
2、deleteCharAt方法
public StringBuffer deleteCharAt(int index)
该方法的作用是删除指定位置的字符,然后将剩余的内容形成新的字符串。

还存在一个功能类似的delete方法:
public StringBuffer delete(int start,int end)
该方法的作用是删除指定区间以内的所有字符,包含start,不包含end索引值的区间。
3、insert方法
public StringBuffer insert(int offset, boolean b)
该方法的作用是在StringBuffer对象中插入内容,然后形成新的字符串。
4、reverse方法
public StringBuffer reverse()
该方法的作用是将StringBuffer对象中的内容反转,然后形成新的字符串。
5、setCharAt方法
public void setCharAt(int index, char ch)
该方法的作用是修改对象中索引值为index位置的字符为新的字符ch。
6、trimToSize方法
public void trimToSize()
该方法的作用是将StringBuffer对象的中存储空间缩小到和字符串长度一样的长度,减少空间的浪费

  • 泛型:
      1)使用?可以接收任意泛型对象。
      2)泛型的上限:?extends 类型。
      3)泛型的下限:?super 类型。
      4)了解为什么泛型子类之间的继承无法直接转换的原因
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,806评论 18 399
  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 3,941评论 1 10
  • 1. 面向对象的特征有哪些方面? 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方...
    程序熊大阅读 4,501评论 6 74
  • Java8张图 11、字符串不变性 12、equals()方法、hashCode()方法的区别 13、...
    Miley_MOJIE阅读 3,759评论 0 11
  • 西贝卖的是莜面吗? No No No 贾总卖的是一种生活方式,背后是绿色、健康…… 大董卖的是烤鸭吗? No No...
    恢恢_小慧阅读 311评论 0 0