201621123086 Java programming 11th Week of study summary

Source: Internet
Author: User
Tags thread class

1. Summary of this week's study

2. Written assignment 1. Source code reading: What is the use of multi-threaded BounceThread1.1 ballrunnable classes? Why do I need to call Thread.Sleep to hibernate in my code?


    • BallRunnableThe interface is completed Runnable . There are four properties jumping balls Ball , forms Component , bounces, STEPS and delays in a class DELAY .
    • The call Thread.sleep was made to allow the thread to hibernate briefly so that we could see clearly the movement of the ball.
1.2 Ball.java only do two things, what are the two things respectively? What does the Ballcomponent object do? What is the use of the ArrayList inside? How many ballcomponent objects are generated during the program run? The program uses multithreading techniques, each of which is drawn in separate threads?


    • move()moving methods and getShape() getting size and coordinates, respectively
    • Add a Ball object and display a small ball object in a form
    • ArrayList for storing small balls
    • An object was generated
    • Each ball is drawn in different threads, respectively.
1.3 Choose: Program rewrite: When the program runs, each ball is from a fixed position. How do you rewrite the program so that when you click Start, each ball can be moved from a different position, with a different step?

A : you can see from the code that Ball the x y starting position dx``dy represents the step size. The call Math.random() can be randomly set to start at different positions and move in different steps.

2. Experimental Summary: Title set (Multithreading) 2.1 Topics: Thread, Printtask, runnable, and anonymous inner classes.

and answer: A) What is the benefit of implementing multithreaded programs by defining the implementation class of the Runnable interface than by inheriting from the thread class? b) 6-1,6-3,6-11 experimental summary.

    • A) A class can complete multiple interfaces, but only one parent class. At the same time, runnable interface is more suitable for resource sharing.
    • b
      6-1: inherit the thread class and complete the Run method
      6-3: Writing an anonymous inner class that completes the Runnable interface
      6-11: Write a class that completes the Runnable interface
2.2 Using a lambda expression to overwrite 6-3
    Thread t = new Thread(() -> {        System.out.println(mainThreadName);        System.out.println(Thread.currentThread().getName());        System.out.println(Arrays.toString(Thread.class.getInterfaces()));    });
2.3 Title: 6-2 (runnable and stop thread). Answer: How do I need to properly stop a running thread?


    • Method can be called stop()
    • Method can be called interupt()
    • Flag can be set as run() the method loop
2.6 Choose: More difficult: 6-10 (callable), and answer why Runnable need callable? Experimental summary.
    • Experimental summary: The points to note in this experiment include: The call() method returns an integer instead of the underlying type, and future.get() you should pay attention to handling the exception when calling the method.
    • Runnable Callable You can return a value compared to. It is important to note that the call method returns the future object.
3. Mutex Access 3.1 Modify the Testunsynchronizedthread.java source code so that it can be accessed synchronously. (Key code, need to appear number)

201621123086 Java programming 11th Week of study summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.