Object类join()方法解析

join方法通常用来做同步操作,如果线程1在线程2之后执行,就可以在线程1中调用线程2.join()方法来实现。实际上这个操作我们用wait/notify也是可以实现的。

@Slf4j
public class Demo {
    private static Demo lock = new Demo();

    public static void main(String[] args) throws Throwable {
        new Thread(() -> {
            synchronized (lock) {
                try {
                    log.info("我是线程1,我需要先执行");
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.notifyAll();
            }
        }).start();

        synchronized (lock) {
            lock.wait();
        }

        log.info("我是线程2,我需要在线程1之后执行");
    }
}

结果

09:25:55.705 com.ailik.d1.wait_notify.Demo [Thread-0] - 我是线程1,我需要先执行
09:25:57.709 com.ailik.d1.wait_notify.Demo [main] - 我是线程2,我需要在线程1之后执行

进程已结束,退出代码为 0

接下来使用join方法实现

@Slf4j
public class Demo {
    @SneakyThrows
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            try {
                log.info("我是线程1,我需要先执行");
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();

        t1.join();

        log.info("我是线程2,我需要在线程1之后执行");
    }
}

实际上join()方法内部同样是wait/notify的操作。接下来我们来看一下join()的源码(这里只贴重要部分)

public final synchronized void join(long millis)
    while (isAlive()) {
        wait(0);
    }    
}

从源码中我们可以得到的以下信息

1、这是一个同步方法(同步锁是线程对象,也就是线程2的线程对象)
2、里面调用了wait方法,wait方法作用是使当前线程进入锁对象的等待池(注意:当前线程是线程1。因为代码是在线程1中调用的join方法)

线程1调用join方法后,会被wait方法阻塞,但是似乎并没有一个可以唤醒线程1的notify方法。但是从效果来看,这个notify方法应该是存在的。之后我又通过查询资料,最后去翻看jdk源码发现确实线程结束之后调用了notifyAll方法(这里也是只贴关键方法)

void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
  ensure_join(this);
}

static void ensure_join(JavaThread* thread) {
  lock.notify_all(thread);
}

那么就结案了
线程1调用线程2.join()方法后会被阻塞,同步锁是线程2对象。当线程2执行完后又会调用notifyAll来唤醒线程2等待池中的所有线程对象


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1、HttpUtils Http网络工具,主要包括httpGet、httpPost以及http参数相关方法,以httpGet为例: static HttpResponse httpGet(HttpRequest request) static HttpResponse httpGet(java.lang.String httpUrl) static String httpGetString(String httpUrl) 包含以上三个方法,默认使用gzip压缩,使用bufferedReader提高读取速度。 HttpRequest中可以设置url、timeout、userAgent等其他http参数 HttpResponse中可以获取返回内容、http响应码、http过期时间(Cache-Control的max-age和expires)等 前两个方法可以进行高级参数设置及丰富内容返回,第三个方法可以简单的传入url获取返回内容,httpPost似。更详细的设置可以直接使用HttpURLConnection或apache的HttpClient。 源码可见HttpUtils.java,更多方法及更详细参数介绍可见HttpUtils Api Guide。 2、DownloadManagerPro Android系统下载管理DownloadManager增强方法,可用于包括获取下载相关信息,如: getStatusById(long) 得到下载状态 getDownloadBytes(long) 得到下载进度信息 getBytesAndStatus(long) 得到下载进度信息和状态 getFileName(long) 得到下载文件路径 getUri(long) 得到下载uri getReason(long) 得到下载失败或暂停原因 getPausedReason(long) 得到下载暂停原因 getErrorCode(long) 得到下载错误码 源码可见DownloadManagerPro.java,更多方法及更详细参数介绍可见DownloadManagerPro Api Guide。关于Android DownManager使用可见DownManager Demo。 3、ShellUtils Android Shell工具,可用于检查系统root权限,并在shell或root用户下执行shell命令。如: checkRootPermission() 检查root权限 execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) shell环境执行命令,第二个参数表示是否root权限执行 execCommand(String command, boolean isRoot) shell环境执行命令 源码可见ShellUtils.java,更多方法及更详细参数介绍可见ShellUtils Api Guide。关于静默安装可见apk-root权限静默安装。 4、PackageUtils Android包相关工具,可用于(root)安装应用、(root)卸载应用、判断是否系统应用等,如: install(Context, String) 安装应用,如果是系统应用或已经root,则静默安装,否则一般安装 uninstall(Context, String) 卸载应用,如果是系统应用或已经root,则静默卸载,否则一般卸载 isSystemApplication(Context, String) 判断应用是否为系统应用 源码可见PackageUtils.java,更多方法及更详细参数介绍可见ShellUtils Api Guide。关于静默安装可见apk-root权限静默安装。 5、PreferencesUtils Android SharedPreferences相关工具,可用于方便的向SharedPreferences中读取和写入相关型数据,如: putString(Context, String, String) 保存string型数据 putInt(Context, String, int) 保存int型数据 getString(Context, String) 获取string型数据 getInt(Context, String) 获取int型数据 可通过修改PREFERENCE_NAME变量修改preference name 源码可见PreferencesUtils.java,更多方法及更详细参数介绍可见PreferencesUtils Api Guide。 6、JSONUtils JSONUtils工具,可用于方便的向Json中读取和写入相关型数据,如: String getString(JSONObject jsonObject, String key, String defaultValue) 得到string型value String getString(String jsonData, String key, String defaultValue) 得到string型value 表示从json中读取某个String型key的值 getMap(JSONObject jsonObject, String key) 得到map getMap(String jsonData, String key) 得到map 表示从json中读取某个Map型key的值 源码可见JSONUtils.java,更多方法及更详细参数介绍可见JSONUtils Api Guide。 7、FileUtils 文件工具,可用于读写文件及对文件进行操作。如: readFile(String filePath) 读文件 writeFile(String filePath, String content, boolean append) 写文件 getFileSize(String path) 得到文件大小 deleteFile(String path) 删除文件 源码可见FileUtils.java,更多方法及更详细参数介绍可见FileUtils Api Guide。 8、ResourceUtils Android Resource工具,可用于从android资源目录的raw和assets目录读取内容,如: geFileFromAssets(Context context, String fileName) 得到assets目录下某个文件内容 geFileFromRaw(Context context, int resId) 得到raw目录下某个文件内容 源码可见ResourceUtils.java,更多方法及更详细参数介绍可见ResourceUtils Api Guide。 9、StringUtils String工具,可用于常见字符串操作,如: isEmpty(String str) 判断字符串是否为空或长度为0 isBlank(String str) 判断字符串是否为空或长度为0 或由空格组成 utf8Encode(String str) 以utf-8格式编码 capitalizeFirstLetter(String str) 首字母大写 源码可见StringUtils.java,更多方法及更详细参数介绍可见StringUtils Api Guide。 10、ParcelUtils Android Parcel工具,可用于从parcel读取或写入特殊型数据,如: readBoolean(Parcel in) 从pacel中读取boolean型数据 readHashMap(Parcel in, ClassLoader loader) 从pacel中读取map型数据 writeBoolean(boolean b, Parcel out) 向parcel中写入boolean型数据 writeHashMap(Map map, Parcel out, int flags) 向parcel中写入map型数据 源码可见ParcelUtils.java,更多方法及更详细参数介绍可见ParcelUtils Api Guide。 11、RandomUtils 随机数工具,可用于获取固定大小固定字符内的随机数,如: getRandom(char[] sourceChar, int length) 生成随机字符串,所有字符均在某个字符串内 getRandomNumbers(int length) 生成随机数字 源码可见RandomUtils.java,更多方法及更详细参数介绍可见RandomUtils Api Guide。 12、ArrayUtils 数组工具,可用于数组常用操作,如: isEmpty(V[] sourceArray) 判断数组是否为空或长度为0 getLast(V[] sourceArray, V value, V defaultValue, boolean isCircle) 得到数组中某个元素前一个元素,isCircle表示是否循环 getNext(V[] sourceArray, V value, V defaultValue, boolean isCircle) 得到数组中某个元素下一个元素,isCircle表示是否循环 源码可见ArrayUtils.java,更多方法及更详细参数介绍可见ArrayUtils Api Guide。 13、ImageUtils 图片工具,可用于Bitmap, byte array, Drawable之间进行转换以及图片缩放,目前功能薄弱,后面会进行增强。如: bitmapToDrawable(Bitmap b) bimap转换为drawable drawableToBitmap(Drawable d) drawable转换为bitmap drawableToByte(Drawable d) drawable转换为byte scaleImage(Bitmap org, float scaleWidth, float scaleHeight) 缩放图片 源码可见ImageUtils.java,更多方法及更详细参数介绍可见ImageUtils Api Guide。 14、ListUtils List工具,可用于List常用操作,如: isEmpty(List sourceList) 判断List是否为空或长度为0 join(List list, String separator) List转换为字符串,并以固定分隔符分割 addDistinctEntry(List sourceList, V entry) 向list中添加不重复元素 源码可见ListUtils.java,更多方法及更详细参数介绍可见ListUtils Api Guide。 15、MapUtils Map工具,可用于Map常用操作,如: isEmpty(Map sourceMap) 判断map是否为空或长度为0 parseKeyAndValueToMap(String source, String keyAndValueSeparator, String keyAndValuePairSeparator, boolean ignoreSpace) 字符串解析为map toJson(Map map) map转换为json格式 源码可见MapUtils.java,更多方法及更详细参数介绍可见MapUtils Api Guide。 16、ObjectUtils Object工具,可用于Object常用操作,如: isEquals(Object actual, Object expected) 比较两个对象是否相等 compare(V v1, V v2) 比较两个对象大小 transformIntArray(int[] source) Integer 数组转换为int数组 源码可见ObjectUtils.java,更多方法及更详细参数介绍可见ObjectUtils Api Guide。 17、SerializeUtils 序列化工具,可用于序列化对象到文件或从文件反序列化对象,如: deserialization(String filePath) 从文件反序列化对象 serialization(String filePath, Object obj) 序列化对象到文件 源码可见SerializeUtils.java,更多方法及更详细参数介绍可见SerializeUtils Api Guide。 18、SystemUtils 系统信息工具,可用于得到线程池合适的大小,目前功能薄弱,后面会进行增强。如: getDefaultThreadPoolSize() 得到跟系统配置相符的线程池大小 源码可见SystemUtils.java,更多方法及更详细参数介绍可见SystemUtils Api Guide。 19、TimeUtils 时间工具,可用于时间相关操作,如: getCurrentTimeInLong() 得到当前时间 getTime(long timeInMillis, SimpleDateFormat dateFormat) 将long转换为固定格式时间字符串 源码可见TimeUtils.java,更多方法及更详细参数介绍可见TimeUtils Api Guide。
解析SVN路径下的.java文件并获取改动方法,您可以使用JavaSVN库和JavaParser库。 JavaSVN库是一个Java库,用于访问Subversion存储库。您可以使用这个库来检索文件历史记录和版本信息。 JavaParser库是一个Java库,用于解析和分析Java源代码。您可以使用这个库来解析.java文件并获取文件中的方法。 以下是一个简单的示例代码,展示如何使用这两个库来实现这个任务: ```java import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.IOUtils; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.diff.Edit; import org.eclipse.jgit.diff.EditList; import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.AbstractTreeIterator; import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.TreeWalk; import com.github.javaparser.JavaParser; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.stmt.Statement; public class SVNJavaParser { public static void main(String[] args) throws Exception { String repoUrl = "svn://example.com/svn/myproject"; String username = "myusername"; String password = "mypassword"; String filePath = "/src/main/java/com/example/MyClass.java"; String commitId = "12345"; Repository repository = SVNUtils.getRepository(repoUrl, username, password); RevWalk revWalk = new RevWalk(repository); ObjectId objectId = repository.resolve(commitId); RevCommit commit = revWalk.parseCommit(objectId); AbstractTreeIterator oldTreeParser = SVNUtils.prepareTreeParser(repository, commit.getParent(0)); AbstractTreeIterator newTreeParser = SVNUtils.prepareTreeParser(repository, commit); List<DiffEntry> diffs = new Git(repository).diff() .setOldTree(oldTreeParser) .setNewTree(newTreeParser) .call(); for (DiffEntry entry : diffs) { if (entry.getNewPath().equals(filePath)) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DiffFormatter formatter = new DiffFormatter(outputStream); formatter.setRepository(repository); formatter.format(entry); String diffString = outputStream.toString(); RawText oldRawText = new RawText(diffString.split("@@")[0]); RawText newRawText = new RawText(diffString.split("@@")[2]); EditList editList = new EditList(); editList.addAll(new EditList(oldRawText, newRawText).getEdits()); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(commit.getTree()); treeWalk.setRecursive(true); while (treeWalk.next()) { if (filePath.equals(treeWalk.getPathString())) { CanonicalTreeParser oldTreeParser2 = new CanonicalTreeParser(); ObjectId oldTree = commit.getParent(0).getTree(); try (ObjectReader reader = repository.newObjectReader()) { oldTreeParser2.reset(reader, oldTree); } String oldContent = new String(repository.open(treeWalk.getObjectId(0)).getBytes()); CompilationUnit oldCompilationUnit = JavaParser.parse(oldContent); CompilationUnit newCompilationUnit = null; for (Edit edit : editList) { int beginLine = edit.getBeginA(); int endLine = edit.getEndA(); if (beginLine < 0 || endLine < 0) continue; List<String> oldLines = new ArrayList<>(); for (int i = beginLine; i < endLine; i++) { oldLines.add(oldRawText.getString(i)); } String oldMethodContent = String.join("\n", oldLines); MethodDeclaration oldMethodDeclaration = getMethodDeclaration(oldCompilationUnit, oldMethodContent); if (oldMethodDeclaration == null) continue; int newBeginLine = edit.getBeginB(); int newEndLine = edit.getEndB(); if (newBeginLine < 0 || newEndLine < 0) continue; List<String> newLines = new ArrayList<>(); for (int i = newBeginLine; i < newEndLine; i++) { newLines.add(newRawText.getString(i)); } String newMethodContent = String.join("\n", newLines); if (newCompilationUnit == null) { String newContent = new String(repository.open(treeWalk.getObjectId(0)).getBytes()); newCompilationUnit = JavaParser.parse(newContent); } MethodDeclaration newMethodDeclaration = getMethodDeclaration(newCompilationUnit, newMethodContent); if (newMethodDeclaration == null) continue; System.out.println("Old method: " + oldMethodDeclaration.getNameAsString()); System.out.println("New method: " + newMethodDeclaration.getNameAsString()); } } } } } } private static MethodDeclaration getMethodDeclaration(CompilationUnit compilationUnit, String methodContent) { List<MethodDeclaration> methods = compilationUnit.findAll(MethodDeclaration.class); for (MethodDeclaration method : methods) { String methodString = method.toString(); if (methodString.equals(methodContent)) { return method; } } return null; } } ``` 在这个示例代码中,我们首先使用JavaSVN库来检索指定提交的特定文件的差异。然后,我们使用JavaParser库来解析旧文件中的方法和新文件中的方法,并将它们进行比较,以获取改动方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值