会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133651个问题

代码1:

package com.ljp.sleep_yield_join_stop;

public class MyThread implements Runnable {

    @Override
    public void run() {
//        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "-------------" + i);

        }
    }
}

代码2:

package com.ljp.sleep_yield_join_stop;

public class Test {
    public static void main(String[] args) {
        MyThread mtd = new MyThread();
        Thread t01 = new Thread(mtd, "整天都吃橘子!!!");
        Thread t02 = new Thread(mtd,"想吃肉夹馍!!!");
        t01.start();
        t02.start();
        for(int i=0;i<10;i++){
            if(i==3){
                try {
                    t01.join();
//                    t02.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "*********" + i);
        }
    }
}

老师,是不是可以这么理解:只要是在哪个线程(比如主线程)里调用join(),那这个线程(主线程)阻塞,等其他线程运行完以后,这个线程再运行。那再这个线程(主线程)里,谁调用join()都可以,比如代码里t01也可以,t02也可以,感觉他们效果都是一样的;两个同时调用join(),感觉效果也是一样的,t01和t02并没有先后顺序,作用只是将主线程阻塞了而已。

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 27242楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 27245楼
Python 全系列/第一阶段:Python入门/控制语句 27248楼
Python 全系列/第一阶段:Python入门/控制语句 27249楼

this.size++报错哪里出问题了
/*
* 基于双向链表实现元素存储的容器
* */
public class MyDoublyLinkedList<E> implements MyList<E> {

    /*
    * 定义双向链表的节点对象
    * */
    class Node<E>{
         E item;
         Node<E> prev;      //记录前一个节点对象
         Node<E> next;      // 记录下一个节点对象
         Node(Node<E> prev,E item,Node<E> next){
             this.prev = prev;
             this.item = item;
             this.next = next;
         }
    }
    private  Node head;   //记录头节点
    private  Node tail;   //记录尾节点
    private  Node size;   //记录原元素个数

    /*
    * 向双向链表中添加元素的方法
    * */
    @Override
    public void add(E element) {
        this.linkLast(element);
    }

    /*
    * 将节点对象添加到双向链表尾部
    * */
    private void linkLast(E element){
        //获取尾节点
        Node t = this.tail;
        //创建节点对象
        Node<E> node = new Node<>(t,element,null);
        //将新节点定义为尾节点
        this.tail = node;
        if(t == null){
            this.head = node;
        }else{
            t.next = node;
        }
        this.size++
    }

    /*
    * 根据指定位置获取元素
    * */
    @Override
    public E get(int indxe) {
        return null;
    }

    /*
    * 返回元素个数
    * */
    @Override
    public int size() {
        return 0;
    }

    /*
    * 根据指定位置删除元素*/
    @Override
    public E remove(int index) {
        return null;
    }

    public static void main(String[] args) {

    }
}

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 27251楼
Python 全系列/第一阶段:Python入门/控制语句 27252楼
Python 全系列/第一阶段:Python入门/控制语句 27254楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637