会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132446个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 1262楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 1263楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 1264楼


按照老师的步骤敲,加上synchronized方法后会报错java: illegal start of expression,仔细检查了下好像也没敲错,文档用的表示方式和老师敲的也不太一样,请问是为什么


class Programmer{
    private String name;
    public Programmer(String name){
        this.name = name;
    }

    public void coding() {
        synchronized (){
            try {
                System.out.println(this.name + " double click Idea");
                Thread.sleep(500);
                System.out.println(this.name+" Idea start");
                Thread.sleep(500);
                System.out.println(this.name+" happy coding");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public void computer() {

        synchronized (){
            try {
                System.out.println(this.name + " POWER ON");
                Thread.sleep(500);
                System.out.println(this.name + " press on");
                Thread.sleep(500);
                System.out.println(this.name + " system starting");
                Thread.sleep(500);
                System.out.println(this.name + " System On");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }

}

class Working1 extends Thread{
    private Programmer p;
    public Working1(Programmer p){
        this.p = p;
    }
    @Override
    public void run() {
        this.p.computer();
    }
}


class Working2 extends Thread{
    private Programmer p;
    public Working2(Programmer p){
        this.p = p;
    }
    @Override
    public void run() {
        this.p.coding();
    }
}




public class TestSyncThread {
    public static void main(String[] args) {
        Programmer p = new Programmer("Lee");
        new Working1(p).start();
        new Working2(p).start();

    }


}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 1266楼

package com.bjsxt.ls.DuoXianCheng.线程并发;
/*
线程并发:生产者和消费者模型
定义一个做馒头和取馒头的类,中间有一个缓冲区。做好的馒头放入缓冲区,取馒头从缓冲区取。
假设缓冲区容量是10,那么当做了十个馒头的时候,做馒头线程就要停止(阻塞)。当缓冲区没有馒头的时候,取馒头线程就要停止(阻塞)。
但是当做馒头线程放入馒头的时候,就要用notify提醒取馒头线程,不要一直处于阻塞状态;
同样当取馒头线程拿馒头的时候,也要提醒做馒头线程要做馒头,不要一直处于阻塞状态;
*/

//定义馒头类
class ManTou{
    private int id;
    public ManTou(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }
}

//定义缓冲区,用数组来存放馒头
class resitor{
    
    private ManTou[] arr =new ManTou[10]; //创建一个长度为10,类型为ManTou的数组
    private int index; //定义索引

    //定义做馒头方法
    //因为做馒头和取馒头都是对同样对象进行操作,所以这两个状态是要互斥,即同步的。所以要用synchronized使得这两个状态处于同步状态
    //synchronzied放在方法名上相当于将synchronized(this){}将方法体包裹起来。
    public synchronized void makeMantou(ManTou manTou){
        //用while做出判断,如果当数组满的时候,就要用wait方法,使得此线程进入阻塞状态,不再生产馒头了;
        while (this.index == this.arr.length){
            try {
                wait();    /*wait属于Object类只能用在synchronized块中。
                           此方法执行之后,在本方法所在的对象锁会被阻塞, 其他需要该对象锁的线程就可以继续运行了。*/
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }              //notify也属于Object类
        this.notify();//当放入馒头的时候,要用notify唤醒取馒头的线程,以防拿馒头线程处于阻塞状态 。
                     //该方法会唤醒处于等待状态队列中的一个线程
        this.arr[this.index]=manTou;
        index++;
    }

    //定义取馒头方法
    public synchronized ManTou takeMantou(){
        //用while判断,当索引为0,即缓冲区没有馒头的时候,就用wait阻塞此状态,不要再去取馒头了;
        while (this.index==0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();//拿馒头的时候也要唤醒做馒头的线程,以防处于阻塞状态不做馒头了。
        this.index--;
        return this.arr[this.index];
    }
}


//创建做馒头线程
class makeThread extends Thread{
    private resitor r; //定义一个类型是resitor的变量r
    public makeThread(resitor r){
        this.r = r;
    }

    @Override
    public void run() {
        for (int i=1;i<11;i++){
            System.out.println("生成第"+i+"个馒头");
            ManTou manTou =new ManTou(i);
            this.r.push(manTou);
        }
    }
}

//创建取馒头线程
class takeThread extends Thread{
    private resitor r; //定义一个类型是resitor的变量r
    public takeThread(resitor r){
        this.r = r;
    }

    @Override
    public void run() {
        for (int i=1;i<11;i++){
            ManTou manTou =this.r.pop();
            System.out.println("拿走第"+i+"个馒头");
        }
    }
}

public class ProducerThread {
    public static void main(String[] args) {
        resitor tt =new resitor();
        new makeThread(tt).start();
        new takeThread(tt).start();
    }
}

QQ图片20210425162135.png

老师这是什么原因造成的?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1267楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/智能电话本项目实战 1268楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1269楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 1270楼

老师这个是什么问题

package com.bjxst;

public class MyDoublyLinkedList<E> implements MyList<E> {
    private Node head;
    private Node tail;
    private int size;

    @Override
    public void add(E element) {
        this.linkedLast(element);

    }
    private void linkedLast(E element){
        //首先获取尾部节点
        Node t = this.tail;
        //创建节点对象
        Node node1 =new Node(t,null,element);

        node1=this.tail;
        if(t==null){
            node1 =this.head;
        }else{
            node1 = t.next;
        }
        this.size++;
    }

    @Override
    public E get(int index) {
        //对Index做合法性校验
        this.checkIndex(index);

        //根据节点对象查找位置
        Node<E> node = this.getNode(index);
        return node.item;

    }
    private void checkIndex(int index){
        if(!(index>=0&&index<this.size)){
            throw new IndexOutOfBoundsException("index"+index+"size"+size);
        }
    }
    private Node getNode(int index){
        if (index<(this.size>>1)){
            Node node = this.head;
            for (int i=0;i<index;i++){
                node = node.next;
            }
            return node;
        }else{
            Node node = this.tail;
            for (int i=this.size-1;i>index;i--){
                node = node = node.prev;
            }
            return node;
        }




    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public E remove(int index) {
        //对index进行合法性校验
        this.checkIndex(index);
        //根据指定位置获取节点对象
        Node<E> node = this.getNode(index);
        //获取节点对象中的元素
        E item = node.item;
        //判断当前节点是否为头节点
        if(node.prev ==null){
            node.next=this.head;

        }else{
            node.prev.next = node.next;

        }
        //判断当前节点是否为尾结点
        if(node.next==null){
            node.prev = this.tail;
        }else{
            node.next.prev = node.prev;
        }
        node.next =null;
        node.prev= null;
        node.item =null;

        this.size--;
        return item;


    }
    //创建节点内部类
    class Node<E>{
        E item;
        Node<E> prev;
        Node<E> next;
        Node(Node<E> prev,Node<E> next,E item){
            this.prev = prev;
            this.next=next;
            this.item = item;
        }
    }
    public void addFirst(E element){
        this.linkFirst(element);
    }
    private void linkFirst(E element){
        //获取头结点对象
        Node head = this.head;
        Node node1 = new Node(null,head,element);
        //将新节点定为头节点
        node1 =this.head;

        if(head==null){
            node1 = this.tail;
        }else{
            node1=head.prev;
        }
        this.size++;
    }
    public void addLast(E element){
        this.linkedLast(element);
    }

    public static void main(String[] args) {
        MyList<String> myList = new MyDoublyLinkedList<>();
        myList.add("a");
        myList.add("b");
        myList.add("at");
        myList.add("e");
        myList.add("e");



        System.out.println(myList.remove(3));
    }
}

屏幕截图 2021-08-30 093615.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 1273楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 1274楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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