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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多个小球运动</title><style>canvas{border: 1px solid;}</style>
</head>
<body>
    <canvas width="500px" width="400px">对不起你的浏览器不支持canvas</canvas>
    <script>
        var mycanvas=document.querySelector('canvas');
        var ctx=mycanvas.getContext('2d');
        function Circle(){
            this.r=Math.floor(Math.random()*20+6);
            this.x=Math.floor(Math.random()*(500-this.r));
            this.y=Math.floor(Math.random()*(400-this.r));
            this.dx=Math.floor(Math.random*10);
            this.dy=Math.floor(Math.random*8);
            this.color='rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+')';
        }
        Circle.prototype.yd=function(){
            this.x+=this.dx;
            if(this.x<=this.r){
                this.dx=-this.dx
            }else if(this.x>=500-this.r){
                this.dx=-this.dx
            }
            this.y+=this.dy;
            if(this.y<=this.r){
                this.y=-this.dy
            }else if(this.y>=400-this.r){
                this.dy=-this.dy
            }
        }
       Circle.prototype.abs=function(){
           ctx.beginPath();
           ctx.arc(this.x,this.y,this.r,0,2*Math.PI);
           ctx.fillStyle=this.color;
           ctx.fill();
           ctx.closePath();
         

       }
       var arr=[];
           for(var i=0 ;i<4;i++){
               arr[i]=new Circle();
           }
           var timer=setInterval(function () {
               ctx.clearRect(0,0,mycanvas.width,mycanvas.height)
               for(var i=0;i<arr.length;i++){
                   arr[i].yd();
                   arr[i].abs();

               }
           },40)

    </script>
</body>
</html>

老师为啥我的球显示不了

WEB前端全系列/第九阶段:HTML5新特性模块/(旧)H5新特性 27171楼
微服务/第十八阶段:数字货币交易所项目(Spring Cloud Alibaba架构)/IASS基础服务的搭建和开发 27172楼
微服务/第十八阶段:数字货币交易所项目(Spring Cloud Alibaba架构)/IASS基础服务的搭建和开发 27173楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 27174楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 27175楼

代码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 基础深化和提高/多线程技术(旧) 27179楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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